itertools中的函数大多是返回各种迭代器对象

用于过滤的生成器函数

模块 函数 说明
itertools compress(it, selector_it) 并行处理两个可迭代对象,如果selector_it中的元素是真,产出it中对应的元素
itertools dropwhile(predicate, it) 处理 it,跳过 predicate 的计算结果为真值的元素,然后产出剩下的各个元素(不再进一步检查)
内置 filter(predicate, it) 把 it 中的各个元素传给 predicate,如果 predicate(item) 返回真值,那么产出对应的元素;如果 predicate 是 None,那么只产出真值元素
itertools filterfalse(predicate, it) 与 filter 函数的作用类似,不过 predicate 的逻辑是相反的:predicate 返回假值时产出对应的元素
itertools islice(it, stop) 或 islice(it, start, stop, step=1) 产出 it 的切片,作用类似于 s[:stop] 或 s[start:stop:step],不过 it 可以是任何可迭代的对象,而且这个函数实现的是惰性操作
itertools takewhile(predicate, it) predicate 返回真值时产出对应的元素,然后立即停止,不再继续检查
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import itertools

it = [1, 2, 3, 4, 5]


def f(v):
return v % 2 == 1


print(filter(f, it), list(filter(f, it)))
print(itertools.filterfalse(f, it), list(itertools.filterfalse(f, it)))
print(itertools.dropwhile(f, it), list(itertools.dropwhile(f, it)))
print(itertools.takewhile(f, it), list(itertools.takewhile(f, it)))
print(itertools.compress(it, [True, False, True, False, False]), list(itertools.compress(it, [True, False, True, False, False])))
print(itertools.islice(it, 3), list(itertools.islice(it, 3)))
print(itertools.islice(it, 3, 5), list(itertools.islice(it, 3, 5)))
print(itertools.islice(it, 1, 5, 2), list(itertools.islice(it, 1, 5, 2)))
###
<filter object at 0x10a00d550> [1, 3, 5]
<itertools.filterfalse object at 0x10a00d550> [2, 4]
<itertools.dropwhile object at 0x10a4636c8> [2, 3, 4, 5]
<itertools.takewhile object at 0x10a4636c8> [1]
<itertools.compress object at 0x10a00d550> [1, 3]
<itertools.islice object at 0x109dc7d68> [1, 2, 3]
<itertools.islice object at 0x109dc7d68> [4, 5]
<itertools.islice object at 0x109dc7d68> [2, 4]

用于映射的生成器函数

模块 函数 说明
itertools accumulate(it, [func]) 把前两个元素传给func,如果没有提供func,默认是add。然后把计算结果和下一个元素传给它,以此类推,最后产出结果
内置 enumerate(iterable, start=0) 产出由两个元素组成的元组,结构是 (index, item),其中 index 从 start 开始计数,item 则从 iterable 中获取
内置 map(func, it1, [it2, …, itN]) 把 it 中的各个元素传给func,产出结果;如果传入 N 个可迭代的对象,那么 func 必须能接受 N 个参数,而且要并行处理各个可迭代的对象
itertools starmap(func, it) 把 it 中的各个元素传给 func,产出结果;输入的可迭代对象应该产出可迭代的元素 iit,然后以 func(*iit) 这种形式调用 func
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import itertools
import operator
it = [1, 2, 3, 4, 5]

def pt(m):
print(m,list(m))

m = itertools.accumulate(it)
pt(m)
m = itertools.accumulate(it,operator.mul)
pt(m)
m = enumerate(it,5)
pt(m)
m = map(lambda x:x*2,it)
pt(m)
m = map(lambda x,y:x+2*y,it,it[::-1])
pt(m)
m =itertools.starmap(lambda *x:x,enumerate(it,5))
pt(m)
###
<itertools.accumulate object at 0x1072a2848> [1, 3, 6, 10, 15]
<itertools.accumulate object at 0x1072a28c8> [1, 2, 6, 24, 120]
<enumerate object at 0x105fa4b88> [(5, 1), (6, 2), (7, 3), (8, 4), (9, 5)]
<map object at 0x105b59828> [2, 4, 6, 8, 10]
<map object at 0x105f97588> [11, 10, 9, 8, 7]
<itertools.starmap object at 0x105b59550> [(5, 1), (6, 2), (7, 3), (8, 4), (9, 5)]

合并多个可迭代对象的生成器函数

模块 函数 说明
itertools chain(it1, …, itN) 先产出 it1 中的所有元素,然后产出 it2 中的所有元素,以此类推,无缝连接在一起
itertools chain.from_iterable(it) 产出 it 生成的各个可迭代对象中的元素,一个接一个,无缝连接在一起;it 应该产出可迭代的元素,例如可迭代的对象列表
itertools product(it1, …, itN, repeat=1) 计算笛卡儿积:从输入的各个可迭代对象中获取元素,合并成由 N 个元素组成的元组,与嵌套的 for 循环效果一样;repeat 指明重复处理多少次输入的可迭代对象
内置 zip(it1, …, itN) 并行从输入的各个可迭代对象中获取元素,产出由 N 个元素组成的元组,只要有一个可迭代的对象到头了,就默默地停止
itertools zip_longest(it1, …, itN, fillvalue=None) 并行从输入的各个可迭代对象中获取元素,产出由 N 个元素组成的元组,等到最长的可迭代对象到头后才停止,空缺的值使用 fillvalue 填充
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import itertools
import operator

it1 = range(1, 5)
it2 = range(6, 10)

def pt(m):
print(m,list(m))

m = itertools.chain(it1,it2)
pt(m)
m = itertools.chain.from_iterable([it1,it2])
pt(m)
m = itertools.product([1,2],[3,4],repeat=2)
pt(m)
m = zip([1,2],[3,4,5])
pt(m)
m = itertools.zip_longest([1,2],[3,4,5],fillvalue='z')
pt(m)
###
<itertools.chain object at 0x10b13b630> [1, 2, 3, 4, 6, 7, 8, 9]
<itertools.chain object at 0x10b13b588> [1, 2, 3, 4, 6, 7, 8, 9]
<itertools.product object at 0x10b148b88> [(1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 3), (1, 3, 2, 4), (1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 3), (1, 4, 2, 4), (2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 3), (2, 3, 2, 4), (2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 3), (2, 4, 2, 4)]
<zip object at 0x10c4467c8> [(1, 3), (2, 4)]
<itertools.zip_longest object at 0x10c40f598> [(1, 3), (2, 4), ('z', 5)]

输入扩展多输出的生成器函数

模块 函数 说明
itertools combinations(it, out_len) 把 it 产出的 out_len 个元素组合在一起,然后产出
itertools combinations_with_replacement(it, out_len) 把 it 产出的 out_len 个元素组合在一起,然后产出,包含相同元素的组合
itertools count(start=0, step=1) 从 start 开始不断产出数字,按 step 指定的步幅增加
itertools cycle(it) 从 it 中产出各个元素,存储各个元素的副本,然后按顺序重复不断地产出各个元素
itertools permutations(it, out_len=None) 把 out_len 个 it 产出的元素排列在一起,然后产出这些排列;out_len 的默认值等于 len(list(it))
itertools repeat(item, [times]) 重复不断地产出指定的元“素,除非提供 times,指定次数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import itertools

it = [1,2,3]

def pt(m):
print(m,list(m))

m = itertools.combinations(it, 2)
pt(m)
m = itertools.combinations_with_replacement(it, 3)
pt(m)
m = itertools.count(1,2)
print(m,itertools.islice(m,10),list(itertools.islice(m,10)))
m = itertools.cycle(it)
print(m,itertools.islice(m,10),list(itertools.islice(m,10)))
m = itertools.permutations(it)
pt(m)
m = itertools.repeat(it, 3)
pt(m)
###
<itertools.combinations object at 0x10bdb1d68> [(1, 2), (1, 3), (2, 3)]
<itertools.combinations_with_replacement object at 0x10bf2c5e8> [(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 2), (1, 2, 3), (1, 3, 3), (2, 2, 2), (2, 2, 3), (2, 3, 3), (3, 3, 3)]
count(21, 2) <itertools.islice object at 0x10bf2c5e8> [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
<itertools.cycle object at 0x10c372b88> <itertools.islice object at 0x10bf2c5e8> [1, 2, 3, 1, 2, 3, 1, 2, 3, 1]
<itertools.permutations object at 0x10d60fd58> [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
repeat([1, 2, 3], 0) [[1, 2, 3], [1, 2, 3], [1, 2, 3]]

重新排列元素的生成器函数

模块 函数 说明
itertools groupby(it,key=None) 产出由两个元素组成的元素,形式为 (key, group),其中 key 是分组标准,group 是生成器,用于产出分组里的元素
内置 reversed(seq) 从后向前,倒序产出 seq 中的元素;seq 必须是序列,或者是实现了 __reversed__ 特殊方法的对象
itertools tee(it,n=2) 产出一个由 n 个生成器组成的元组,每个生成器用于单独产出输入的可迭代对象中的元素
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import itertools

it = ['a','ab','ac','aba']

def pt(m):
print(m,list(m))

m = itertools.groupby(it)
for i in m:
print(i[0],end=' ')
pt(i[1])

pt(reversed(it))

m = itertools.tee(it,3)
for i in m:
for j in i:
print(j,end=',')
print()
###
a <itertools._grouper object at 0x103eb65c0> ['a']
ab <itertools._grouper object at 0x103eb6898> ['ab']
ac <itertools._grouper object at 0x103eb65c0> ['ac']
aba <itertools._grouper object at 0x103eb6898> ['aba']
<list_reverseiterator object at 0x103eb65c0> ['aba', 'ac', 'ab', 'a']
a,ab,ac,aba,
a,ab,ac,aba,
a,ab,ac,aba,