一、itertools(itertools 模块提供了很多用于产生多种类型迭代器的函数,它们的返回值不是 list,而是迭代器。)
Python 内置的 itertools 模块包含了一系列用来产生不同类型迭代器的函数或类,这些函数的返回都是一个迭代器,我们可以通过 for 循环来遍历取值,也可以使用 next() 来取值。
二、无限迭代器
(一)count() 接收两个参数,第一个参数指定开始值,默认为 0,第二个参数指定步长,默认为 1:
import itertools
nums = itertools.count()
for num in nums:
if num > :
break
print num
# 0,1,2
(二)cycle() 元素反复执行循环
import itertools
numbers = itertools.cycle("123")
i =
data = list()
for num in numbers:
if i == :
break
i +=
data.extend((i, num))
print data
# [1, '1', 2, '2', 3, '3', 4, '1', 5, '2', 6, '3']
(三)repeat() 反复生成一个 object
import itertools
datas = itertools.repeat('nihao', )
for data in datas:
print data
# nihao
# nihao
# nihao
三、有限迭代器
(一)chain 接收多个可迭代对象作为参数,将它们连接起来,作为一个新的迭代器返回。
from itertools import chain
for item in chain([, , ], ['a', 'b', 'c']):
print item
...
a
b
c
(二)compress(data, selectors) 对数据进行筛选,当 selectors 的某个元素为 true 时,则保留 data 对应位置的元素,否则去除。
>>> from itertools import compress
>>>
>>> list(compress('ABCDEF', [, , , , , ]))
['A', 'B', 'D', 'F']
>>> list(compress('ABCDEF', [, , , ]))
['A', 'B', 'D']
>>> list(compress('ABCDEF', [True, False, True]))
['A', 'C']
(三)dropwhile(predicate, iterable) predicate 是函数,iterable 是可迭代对象。对于 iterable 中的元素,如果 predicate(item) 为 true,则丢弃该元素,否则返回该项及所有后续项。
>>> from itertools import dropwhile
>>>
>>> list(dropwhile(lambda x: x < , [, , , , ]))
[, , ]
(四)islice(iterable, [start,] stop [, step]) iterable 是可迭代对象,start 是开始索引,stop 是结束索引,step 是步长,start 和 step 可选。
>>> from itertools import islice
>>>
>>> list(islice([, , , , , , ], ))
[, , , , ]
(五)imap(func, iter1, iter2, iter3, …) imap 返回一个迭代器,元素为 func(i1, i2, i3, …),i1,i2 等分别来源于 iter, iter2。
>>> from itertools import imap
>>>
>>> imap(str, [, , , ])
<itertools.imap object at >
>>>
>>> list(imap(str, [, , , ]))
['1', '2', '3', '4']
>>>
>>> list(imap(pow, [, , ], [, , ]))
[, , ]
(六)tee(iterable [,n]) 用于从 iterable 创建 n 个独立的迭代器,以元组的形式返回,n 的默认值是 2。
>>> from itertools import tee
>>>
>>> tee('abcd') # n 默认为 2,创建两个独立的迭代器
(<itertools.tee object at >, <itertools.tee object at >)
>>>
>>> iter1, iter2 = tee('abcde')
>>> list(iter1)
['a', 'b', 'c', 'd', 'e']
>>> list(iter2)
['a', 'b', 'c', 'd', 'e']
>>>
>>> tee('abc', ) # 创建三个独立的迭代器
(<itertools.tee object at >, <itertools.tee object at >, <itertools.tee object at >)
(七)takewhile(predicate, iterable) predicate 是函数,iterable 是可迭代对象。对于 iterable 中的元素,如果 predicate(item) 为 true,则保留该元素,只要 predicate(item) 为 false,则立即停止迭代。
>>> from itertools import takewhile
>>>
>>> list(takewhile(lambda x: x < , [, , , , ]))
[, ]
>>> list(takewhile(lambda x: x > , [, , , , ]))
[]
(八)izip(iter1, iter2, …, iterN) izip 用于将多个可迭代对象对应位置的元素作为一个元组,将所有元组『组成』一个迭代器,并返回。它的使用形式如下。
>>> from itertools import izip
>>>
>>> for item in izip('ABCD', 'xy'):
... print item
...
('A', 'x')
('B', 'y')
>>> for item in izip([, , ], ['a', 'b', 'c', 'd', 'e']):
... print item
...
(, 'a')
(, 'b')
(, 'c')
四、组合生成器
(一)product(iter1, iter2, … iterN, [repeat=1]) 用于求多个可迭代对象的笛卡尔积,它跟嵌套的 for 循环等价。
>>> from itertools import product
>>>
>>> for item in product('ABCD', 'xy'):
... print item
...
('A', 'x')
('A', 'y')
('B', 'x')
('B', 'y')
('C', 'x')
('C', 'y')
('D', 'x')
('D', 'y')
>>>
>>> list(product('ab', range()))
[('a', ), ('a', ), ('a', ), ('b', ), ('b', ), ('b', )]
>>>
>>> list(product((,), (,), (,)))
[(, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , )]
>>>
>>> list(product('ABC', repeat=))
[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('C', 'A'), ('C', 'B'), ('C', 'C')]
(二)permutations(iterable[, r]) 用于生成一个排列。r 指定生成排列的元素的长度,如果不指定,则默认为可迭代对象的元素长度。
>>> from itertools import permutations
>>>
>>> permutations('ABC', )
<itertools.permutations object at >
>>>
>>> list(permutations('ABC', ))
[('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
>>>
>>> list(permutations('ABC'))
[('A', 'B', 'C'), ('A', 'C', 'B'), ('B', 'A', 'C'), ('B', 'C', 'A'), ('C', 'A', 'B'), ('C', 'B', 'A')]
>>>
(三)combinations(iterable, r) 用于求序列的组合。
>>> from itertools import combinations
>>>
>>> list(combinations('ABC', ))
[('A', 'B'), ('A', 'C'), ('B', 'C')]