itertools 模块¶
参考:
更多方便灵活的迭代器工具
无限迭代器¶
itertools.repeat¶
repeat(object [,times])
创建一个迭代器,重复生成 object,times(如果已提供)指定重复计数,如果未提供 times,将无止尽返回该对象
函数式工具¶
itertools.ifilter、itertools.reduce、itertools.imap、itertools.izip
与内建函数 filter(), reduce(), map(), zip() 有同样的功能,只是返回一个迭代器而不是一个序列。在 Python3 中被移除,因为默认的内建函数就是返回一个迭代器。
accumulate¶
itertools.accumulate(iterable[, func])
生成一个累积和序列
accumulate([1,2,3,4,5]) --> 1 3 6 10 15
filterfalse¶
filterfalse(function or None, sequence)
生成 sequence 中 function(item) 为 False 的项
>>> for elem in itertools.filterfalse(lambda x: x > 5, [2, 3, 5, 6, 7]):
>>> print(elem)
2
3
5
zip_longest¶
zip_longest(iter1 [,iter2 [...]], [fillvalue=None])
与 zip 类似,但不同的是它会把最长的 iter 迭代完才结束,其他 iter 如果有缺失值则用 fillvalue 填充
>>> for item in itertools.zip_longest('abcd', '12', fillvalue='-'):
>>> print(item)
('a', '1')
('b', '2')
('c', '-')
('d', '-')
starmap¶
starmap(function, sequence)
对序列 sequence 的每个元素作为 function 的参数列表执行,即 function(*item), 返回执行结果的迭代器
>>> for item in itertools.starmap(lambda x,y:(x, y, x*y), seq):
>>> print(item)
(0, 5, 0)
(1, 6, 6)
(2, 7, 14)
(3, 3, 9)
(3, 8, 24)
(4, 9, 36)
dropwhile¶
dropwhile(predicate, iterable)
丢弃在 predicate 第一次为 False 之前的所有项,其后的所有项都保留
>>> for item in itertools.dropwhile(lambda x: x<1, [ -1, 0, 1, 2, 3, 4, 1, -2 ]):
>>> print(item)
1
2
3
4
1
-2
takewhile¶
takewhile(predicate, iterable)
保留 predicate 为 True 的项,只要 predicate 一为 False 迭代就立即停止
>>> for item in itertools.takewhile(lambda x: x < 2, [ -1, 0, 1, 2, 3, 4, 1, -2 ]):
>>> print(item)
-1
0
1
组合工具¶
itertools.chain¶
chain(*iterables)
把一组迭代对象串联起来,形成一个更大的迭代器
for c in itertools.chain('ABC', 'XYZ'):
print(c)
A
B
C
X
Y
Z
itertools.product¶
product(*iterables, repeat=1)
创建一个迭代器,生成多个迭代器集合的笛卡尔积,repeat 参数用于指定重复生成序列的次数
for elem in itertools.product((1, 2), ('a', 'b')):
print(elem)
(1, 'a')
(1, 'b')
(2, 'a')
(2, 'b')
itertools.permutations¶
permutations(iterable[, r])
返回 iterable 中任意取 r 个元素做排列的元组的迭代器,如果不指定 r,那么序列的长度与 iterable 中的项目数量相同。
>>> for elem in itertools.permutations('abc', 2):
>>> print(elem)
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'c')
('c', 'a')
('c', 'b')
itertools.combinations¶
combinations(iterable, r)
与 permutations 类似,但组合不分顺序,即如果 iterable 为 “abc”,r 为 2 时,ab 和 ba 则视为重复,此时只放回 ab.
>>> for elem in itertools.combinations('abc', 2):
>>> print(elem)
('a', 'b')
('a', 'c')
('b', 'c')
itertools.combinations_with_replacement¶
combinations_with_replacement(iterable, r)
与 combinations 类似,但允许重复值,即如果 iterable 为 “abc”,r 为 2 时,会多出 aa, bb, cc.
>>> for elem in itertools.combinations_with_replacement('abc', 2):
>>> print(elem)
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'b')
('b', 'c')
('c', 'c')
其他工具¶
itertools.compress¶
'''Python compress(data, selectors)
bool 选取,只有当 selectors 对应位置的元素为 true 时,才保留 data 中相应位置的元素,否则移除
'''Python
>>> list(itertools.compress('abcdef', [1, 1, 0, 1, 0, 1]))
['a', 'b', 'd', 'f']
itertools.groupby¶
groupby(iterable[, keyfunc])
对 iterable 中的元素进行分组。keyfunc 是分组函数,用于对 iterable 的连续项进行分组,如果不指定,则默认对 iterable 中的连续相同项进行分组,返回一个 (key, sub-iterator) 的迭代器。
>>> data = ['a', 'bb', 'cc', 'ddd', 'eee', 'f']
>>> for key, value_iter in itertools.groupby(data, len):
print(key, list(value_iter))
1 ['a']
2 ['bb', 'cc']
3 ['ddd', 'eee']
1 ['f']
itertools.islice¶
islice(iterable, [start,] stop [, step])
对迭代器进行切片选择,start 是开始索引,stop 是结束索引,step 是步长,start 和 step 可选。
list(itertools.islice([10, 6, 2, 8, 1, 3, 9], 5))
[10, 6, 2, 8, 1]
itertools.tee¶
tee(iterable, n=2)
从 iterable 创建 n 个独立的迭代器,以元组的形式返回。默认为 2 个:
>>> for item in itertools.tee("abcedf"):
>>> for i in item:
>>> print(i)
a
b
c
e
d
f
a
b
c
e
d
f