itertools内置库
多个iterable连接
chain
1 itertools.chain(*iterable)
将多个序列作为一个单独的序列返回
from itertools import chain c = chain([1, 2, 3], [4, 5], [6, 7, 8]) print list(c) # [1, 2, 3, 4, 5, 6, 7, 8]
在iter中添加判断类
compress
ifiter
ifterfalse
tabkewihe
1 itertools.compress(data, selector)
返回selector为True的data对应元素
import itertools for ele in itertools.compress('qwer', [1,0,1,1]): print ele
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
output:
q
e
r
2 itertools.ifiter(predicate, iterable)
返回predicate结果为True的元素迭代器,如果predicate为None,则返回所有iterable中为True的项
import itertools for ele in itertools.ifilter(lambda x: x > 5, range(8)): print ele
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
ouput:
6
7
3 itertools.ifterfalse(predicate, iterable)
与itertools.ifiter()正好相反
4 itertools.takewhile(predicate, iterable)
如果predicate为真,则返回iterable元素,如果为假则不再返回,break.
import itertools for ele in itertools.takewhile(lambda x: x < 5, range(8)): print ele
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
ouput:
0
1
2
3
4
分类
groupby
1 itertools.groupby(iterable[,key])
其中,iterable 是一个可迭代对象,keyfunc 是分组函数,用于对 iterable 的连续项进行分组,如果不指定,则默认对 iterable 中的连续相同项进行分组,返回一个 (key, sub-iterator)
的迭代器
import itertools for key, ele in itertools.groupby(['abc', 'awe', 'bjf', 'caf', 'bjf'], key=lambda x: x[0]): print key, tuple(ele)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
output: a ('abc', 'awe') b ('bjf',) c ('caf',) b ('bjf',)
list变为iterable
imap
islice
1 itertools.imap(function,*iterables)
相当于迭代器方式的map()
2 itertools.islice(iterable, start,stop[,step])
相当于迭代器方式的切片操作
无限递增
count
cycle
1 itertools.count(start=0,step=1)
返回以start开始,step递增的序列,无限递增
import itertools for each in itertools.count(start=0, step=2): print each
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
output: 1 2 3 . .
2 itertools.cycle(iterable)
将迭代器进行无限迭代
import itertools for each in itertools.cycle('ab'): print each
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
output:
a
b
a
b
.
排列组合
product
permutations
combinations
combinations_with_replacement
1 product(*iterable, **kwars)
用于求多个可迭代对象的笛卡尔积,它跟嵌套的 for 循环等价。它的一般使用形式如下:
from itertools import product p = product('AB', 'abc', '3') print list(p) # [('A', 'a', '3'), ('A', 'b', '3'), ('A', 'c', '3'), ('B', 'a', '3'), ('B', 'b', '3'), ('B', 'c', '3')]
2 permutations(iterable[, r])
排列,其中参数 r 是从iterable中取出的 iter 个数
from itertools import permutations, combinations p = permutations('abc', 2) print list(p) # [('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]
3 combinations(iterable, r)
组合,其中 r 是从 iterable中取出的iter 个数
from itertools import combinations c = combinations('abc', 2) print list(c) # [('a', 'b'), ('a', 'c'), ('b', 'c')]
4 combinations_with_replacement(iterable, r)
combinations_with_replacement
和 combinations
类似,但它生成的组合包含自身元素
from itertools import combinations_with_replacement c = combinations_with_replacement('abc', 2) print list(c) # [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'b'), ('b', 'c'), ('c', 'c')]
本文参考:
FunHacks 的文章 地址:http://funhacks.net/2017/02/13/itertools/