1. 无限迭代器
count(start=0, step=1) 会返回一个无限的整数iterator,每次增加1。可以选择提供起始编号,默认为0。
>>> from itertools import count
>>> for i in zip(count(1), ['a', 'b', 'c']):
... print(i, end=' ')
...
(1, 'a') (2, 'b') (3, 'c')
cycle(iterable)会把传入的一个序列无限重复下去,不过可以提供第二个参数就可以制定重复次数。
>>> from itertools import cycle
>>> for i in zip(range(6), cycle(['a', 'b', 'c'])):
... print(i, end=' ')
...
(0, 'a') (1, 'b') (2, 'c') (3, 'a') (4, 'b') (5, 'c')
repeat(object[, times]) 返回一个元素无限重复下去的iterator,可以提供第二个参数就可以限定重复次数。
>>> from itertools import repeat
>>>list(repeat('abc', 3))
['abc', 'abc', 'abc']
2. 多个迭代对象的排列组合
product(iterators) 将多个iterator进行组合,以元组形式返回所有可能的结果。
>>> from itertools import product
>>> list(product(range(3), 'abc'))
[(0, 'a'), (0, 'b'), (0, 'c'), (1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c')]
permutations(iterable, r=None)返回长度为r的所有可能的组合
>>> from itertools import permutations
>>> list(permutations(range(3), 2))
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
>>> list(permutations('abcd', 3)) #4*3*2=24种可能的组合方式
[('a', 'b', 'c'), ('a', 'b', 'd'), ('a', 'c', 'b'), ('a', 'c', 'd'), ('a', 'd', 'b'), ('a', 'd', 'c'), ('b', 'a', 'c'), ('b', 'a', 'd'), ('b', 'c', 'a'), ('b', 'c', 'd'), ('b', 'd', 'a'), ('b', 'd', 'c'), ('c', 'a', 'b'), ('c', 'a', 'd'), ('c', 'b', 'a'), ('c', 'b', 'd'), ('c', 'd', 'a'), ('c', 'd', 'b'), ('d', 'a', 'b'), ('d', 'a', 'c'), ('d', 'b', 'a'), ('d', 'b', 'c'), ('d', 'c', 'a'), ('d', 'c', 'b')]
combinations(iterable, r) 返回一个iterator,提供iterable中所有元素可能组合的r元组。每个元组中的元素保持与iterable返回的顺序相同。下面的实例中,不同于上面的permutations,a总是在bcd之前,b总是在cd之前,c总是在d之前
>>> from itertools import combinations
>>> list(combinations('abcd',2))
[('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')]
>>>list(combinations('abcd',4))
[('a', 'b', 'c', 'd')]
combinations_with_replacement(iterable, r)函数放宽了一个不同的约束:元素可以在单个元组中重复,即可以出现aa/bb/cc/dd等组合。
>>> from itertools import combinations_with_replacement
>>> list(combinations_with_replacement('abcd', 2))
[('a', 'a'), ('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'b'), ('b', 'c'), ('b', 'd'), ('c', 'c'), ('c', 'd'), ('d', 'd')]
3. 对于序列迭代对象的一些其他操作函数方法
accumulate(iterable[, func])
>>> from itertools import accumulate
>>> import operator
>>> list(accumulate([1, 2, 3, 4, 5], operator.add)) #叠加
[1, 3, 6, 10, 15]
>>> list(accumulate([1, 2, 3, 4, 5], operator.mul)) #累乘
[1, 2, 6, 24, 120]
itertools.chain(*iterables)可以将多个iterable组合成一个iterator。
>>> from itertools import chain
>>> list(chain(range(3),range(3,10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
chain.from_iterable(iterable)和chain类似,但是只是接收单个iterable,然后将这个iterable中的元素组合成一个iterator。
>>> from itertools import chain.from_iterable
>>> list(chain.from_iterable(['012','3456789']))
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
compress(data, selectors)接收两个iterable作为参数,只返回selectors中对应的元素为True的data,当data/selectors之一用尽时停止
>>> list(compress([1, 2, 3, 4, 5], [True, True, False, False, True]))
[1, 2, 5]
zip_longest(*iterables, fillvalue=None)和zip类似,但是zip的缺陷是iterable中的某一个元素被遍历完,整个遍历都会停止,
>>> list(zip_longest(range(3),range(2)))
[(0, 0), (1, 1), (2, None)]
>>> list(zip(range(3),range(2)))
[(0, 0), (1, 1)]
islice(iterable, stop) or islice(iterable, start, stop[, step]) 与Python的字符串和列表切片有一些类似,只是不能对start、start和step使用负值
>>> from itertools import islice
>>> for i in islice(range(100), 0, 100, 10):
... print(i, end=' ')
...
0 10 20 30 40 50 60 70 80 90
tee(iterable, n=2) 返回n个独立的iterator,n默认为2
>>> from itertools import islice, tee
>>> r = islice(count(), 5)
>>> i1, i2 = tee(r)
>>> i1
[0,1,2,3,4]
>>> i2
[0,1,2,3,4]
starmap(func, iterable)假设iterable将返回一个元组流,并使用这些元组作为参数调用func:
>>> from itertools import starmap
>>> import os
>>> iterator = starmap(os.path.join,
... [('/bin', 'python'), ('/usr', 'bin', 'java'),
... ('/usr', 'bin', 'perl'), ('/usr', 'bin', 'ruby')])
>>> list(iterator)
['/bin/python', '/usr/bin/java', '/usr/bin/perl', '/usr/bin/ruby']
filterfalse(predicate, iterable) 与filter()相反,返回所有predicate返回False的元素
itertools.filterfalse(is_even, itertools.count()) =>
1, 3, 5, 7, 9, 11, 13, 15, ...
takewhile(predicate, iterable) 只要predicate返回True,不停地返回iterable中的元素。一旦predicate返回False,iteration将结束
def less_than_10(x):
return x < 10
itertools.takewhile(less_than_10, itertools.count())
=> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
itertools.takewhile(is_even, itertools.count())
=> 0
dropwhile(predicate, iterable) 在predicate返回True时舍弃元素,然后返回其余迭代结果
itertools.dropwhile(less_than_10, itertools.count())
=> 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ...
groupby(iterable[, key])返回一个产生按照key进行分组后的值集合的迭代器.
如果iterable在多次连续迭代中生成了同一项,则会定义一个组,如果将此函数应用一个分类列表,那么分组将定义该列表中的所有唯一项,key(如果已提供)是一个函数,应用于每一项,如果此函数存在返回值,该值将用于后续项而不是该项本身进行比较,此函数返回的迭代器生成元素(key, group),其中key是分组的键值,group是迭代器,生成组成该组的所有项。
即:按照keyfunc函数对序列每个元素执行后的结果分组(每个分组是一个迭代器), 返回这些分组的迭代器
>>> from itertools import *
>>> a = ['aa', 'ab', 'abc', 'bcd', 'abcde']
>>> for i, k in groupby(a, len): #以此方法来统计相同长度DNA(RNA)超爽
... print i, list(k)
...
2 ['aa', 'ab']
3 ['abc', 'bcd']
5 ['abcde']