1. 全部
- count(start=0, step=1)
- repeat(elem [,n])
- accumulate(p[, func])
- chain(p, q, …)
- chain.from_iterable([p, q, …])
- compress(data, selectors)
- dropwhile(pred, seq)
- groupby(iterable[, keyfunc])
- filterfalse(pred, seq)
- islice(seq, [start,] stop [, step])
- starmap(fun, seq)
- tee(it, n=2)
- takewhile(pred, seq)
- zip_longest(p, q, …)
- product(p, q, … [repeat=1])
- permutations(p[, r])
- combinations(p, r)
- combinations_with_replacement(p, r)
本节主要介绍8-18,1-7见上一节内容
2. 详解
"""
@python version: python3.6.1
@author: XiangguoSun
@contact: sunxiangguodut@qq.com
@site: http://blog.csdn.net/github_36326955
@software: PyCharm
@file: suggest5.py
@time: 5/2/2017 5:58 PM
"""
import itertools
"""
for item in iterable, 我们看一下keyfunc(item)的返回值,将返回值相同的列为一组。name为每一组对应的返回值,
group为该组的成员
"""
qs = [{'date' : 1},{'date' : 2}]
[(name, list(group)) for name, group in itertools.groupby(qs, lambda p:p['date'])]
"""
lambda函数的输入p是qs的item, 返回值是qs每一项的'date'属性值。按照这个进行分组。
output:
[(1, [{'date': 1}]), (2, [{'date': 2}])]
"""
a = ['aa', 'ab', 'abc', 'bcd', 'abcde']
for i, k in itertools.groupby(a, len):
print(i, list(k))
"""
output:
2 ['aa', 'ab']
3 ['abc', 'bcd']
5 ['abcde']
"""
def pred(x):
return x < 1
for x in itertools.filterfalse(pred, [-1,0,2,1,-3,2]):
print(x,end=',')
"""
output:
2,1,2
"""
for x in itertools.islice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 1, 8, 2):
print(x, end=',')
"""
output:
1,3,5,7,
"""
values = [(0, 5), (1, 6), (2, 7), (3, 8), (4, 9)]
for i in itertools.starmap(lambda x, y: (x, y, x*y), values):
print('%d * %d = %d' % i)
"""
output:
0 * 5 = 0
1 * 6 = 6
2 * 7 = 14
3 * 8 = 24
4 * 9 = 36
"""
"""
tee(it,n)中,it为一个迭代器,通过tee操作,我们可以得到n个一样的迭代器:
"""
for x in itertools.tee([1,2,3,4,5,6],3):
for i in x:
print(i,end=",")
print()
"""
output:
1,2,3,4,5,6,
1,2,3,4,5,6,
1,2,3,4,5,6,
"""
def pred(x):
return x < 1
for x in itertools.takewhile(pred, [-1,-2,0,3,4,-1,2]):
print(x,end=",")
"""
output:
-1,-2,0,
"""
for x in itertools.zip_longest([0,1,2,3],[4,5],[6,7]):
print(x, end=",")
"""
output:
(0, 4, 6),(1, 5, 7),(2, None, None),(3, None, None),
"""
import itertools
a = (1, 2, 3)
b = ('A', 'B', 'C')
c = itertools.product(a,b,repeat=1)
d = itertools.product(a,b,repeat=2)
for elem in c:
print(elem, end=",")
print()
for e in d:
print(e, end=",")
"""
output:
(1, 'A'),(1, 'B'),(1, 'C'),(2, 'A'),(2, 'B'),(2, 'C'),(3, 'A'),(3, 'B'),(3, 'C'),
(1, 'A', 1, 'A'),(1, 'A', 1, 'B'),(1, 'A', 1, 'C'),
(1, 'A', 2, 'A'),(1, 'A', 2, 'B'),(1, 'A', 2, 'C'),
(1, 'A', 3, 'A'),(1, 'A', 3, 'B'),(1, 'A', 3, 'C'),
(1, 'B', 1, 'A'),(1, 'B', 1, 'B'),(1, 'B', 1, 'C'),
(1, 'B', 2, 'A'),(1, 'B', 2, 'B'),(1, 'B', 2, 'C'),
(1, 'B', 3, 'A'),(1, 'B', 3, 'B'),(1, 'B', 3, 'C'),
(1, 'C', 1, 'A'),(1, 'C', 1, 'B'),(1, 'C', 1, 'C'),
(1, 'C', 2, 'A'),(1, 'C', 2, 'B'),(1, 'C', 2, 'C'),
(1, 'C', 3, 'A'),(1, 'C', 3, 'B'),(1, 'C', 3, 'C'),
(2, 'A', 1, 'A'),(2, 'A', 1, 'B'),(2, 'A', 1, 'C'),
...
"""
"""
全排列
创建一个迭代器,返回iterable中所有长度为r的项目序列,
如果省略了r,那么序列的长度与iterable中的项目数量相同:
返回p中任意取r个元素做排列的元组的迭代器
"""
p="abc"
for x in itertools.permutations(p):
print(x)
"""
output:
('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')
"""
"""
组合,参加ex3.2
"""
p = "abcd"
for x in itertools.combinations(p,2):
print(x)
"""
output:
('a', 'b')
('a', 'c')
('a', 'd')
('b', 'c')
('b', 'd')
('c', 'd')
"""
"""
这也是组合,但是允许重负例如‘abc’中求C(3,2)
在ex3.3中有ab,ac,bc
在本立中则有:aa,ab,ac,bb,bc,cc
"""
p = "abc"
for x in itertools.combinations_with_replacement(p,2):
print(x)
"""
output:
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'b')
('b', 'c')
('c', 'c')
"""