Functional tools for creating and using iterators. 即用于创建和使用迭代器的函数工具。
1. itertools.chain(*iterable)
将多个序列作为一个单独的序列返回。
In [33]: import itertools
In [34]: for each in itertools.chain('i', 'love', 'python'):
...: print(each)
...:
i
l
o
v
e
p
y
t
h
o
n
2. itertools.combinations(iterable, r)
返回指定长度的”组合”。
In [35]: for each in itertools.combinations('abc', 2):
...: print(each)
...:
('a', 'b')
('a', 'c')
('b', 'c')
可以看出,这个是按原字符串顺序组成的组合。
3. itertools.combinations_with_replacement(iterable, r)
返回指定长度的“组合”,组合内元素可重复
In [37]: for each in itertools.combinations_with_replacement('abc', 2):
...: print(each)
...:
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'b')
('b', 'c')
('c', 'c')
4. itertools.product(*iterable[,repeat])
返回指定长度的所有组合,可理解为笛卡尔乘积
In [39]: for each in itertools.product('abc', repeat=2):
...: print(each)
...:
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'b')
('b', 'c')
('c', 'a')
('c', 'b')
('c', 'c')
5. itertools.premutations(iterable[,r])
返回长度为r的排列
In [41]: for each in itertools.permutations('abc', 2):
...: print(each)
...:
...:
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'c')
('c', 'a')
('c', 'b')
6. itertools.compress(data,selector)
返回selector为True的data对应元素
In [42]: for each in itertools.compress('abcd', [1, 0, 1, 0]):
...: print(each)
...:
a
c
7. itertools.count(start=0,step=1)
返回以start开始,step递增的序列,无限递增
In [44]: for each in itertools.count(start=0, step=2):
...: if each > 20:
...: break
...: print(each)
...:
...:
0
2
4
6
8
10
12
14
16
18
20
8. itertools.cycle(iterable)
将迭代器进行无限迭代
In [45]: for each in itertools.cycle('ab'):
...: print(each)
...:
a
b
a
b
a
b
a
b
.
.
.
9. itertools.dropwhile(predicate, iterable)
drop掉第一个predicate为False之前的数据
In [46]: for each in itertools.dropwhile(lambda x: x<5, [2,1,6,8,2,1]):
...: print(each)
...:
6
8
2
1
第一个False是6,drop之前的数据,剩下的就是6, 8, 2, 1
这个功能可以用在 读取文件时,drop掉文件头部的注释。
with open('xxx.txt') as f:
... for line in dropwhile(lambda line: line.startswith('#'), f):
... print(line, end='')
10. itertools.groupby(iterable[,key])
返回一组(key,itera),key为iterable的值,itera为等于key的所有项
In [48]: for key, value in itertools.groupby('aabbbcacc'):
...: print(key, list(value))
...:
a ['a', 'a']
b ['b', 'b', 'b']
c ['c']
a ['a']
c ['c', 'c']
11. itertools.ifilter(predicate, iterable)
创建一个迭代器,只返回 iterable 中 predicate 为 False 的元素。如果 predicate 是 None,返回真值测试为false的元素。
In [52]: for value in itertools.filterfalse(lambda x: x % 2, range(10)):
...: print(value)
...:
0
2
4
6
8
12. itertools.islice(iterable, start,stop[,step])
相当于迭代器方式的切片操作
In [53]: for value in itertools.islice('abcdefg', 1, 4, 2):
...: print(value)
...:
b
d
13. itertools.repeat(object,[,times])
不停的返回object对象,如果指定了times,则返回times次
In [54]: for value in itertools.repeat('a', 2):
...: print(value)
...:
a
a
14. itertools.starmap(function,iterable)
返回function(iter)的值,iter为iterable的元素
In [55]: for value in itertools.starmap(lambda x, y: x * y, [(1, 2), (3, 4)]):
...: print(value)
...:
2
12
In [56]: for value in itertools.starmap(pow, [(2,5), (3,2), (10,3)]):
...: print(value)
...:
...:
32
9
1000
15. itertools.takewhile(predicate,iterable)
如果predicate为真,则返回iterable元素,如果为假则不再返回,break.
In [57]: for value in itertools.takewhile(lambda x: x < 5, [1, 3, 5, 6]):
...: print(value)
...:
1
3
网友评论