Python版本:python3.6.2
参考:http://funhacks.net/2017/02/13/itertools/
itertools模块包含的函数产生的是迭代器,而不是数组。
import itertools
nums = itertools.count()
for i in nums:
if i > 6:
break
print(i)
输出:
0
1
2
3
4
5
6
指定初始值和步长:
import itertools
nums = itertools.count(2, 2)#指定开始值和步长值
for i in nums:
if i > 6:
break
print(i)
输出:
2
4
6
import itertools
cycle_strings = itertools.cycle('ABC')
i = 1
for string in cycle_strings:
if i == 10:
break
print(i, string)
i += 1
输出:
1 A
2 B
3 C
4 A
5 B
6 C
7 A
8 B
9 C
import itertools
for item in itertools.repeat('hello world', 3):
print(item)
输出:
hello world
hello world
hello world
重复的类型可以是字符串、数组等。
import itertools
for item in itertools.repeat([1, 2, 3, 4], 3):
print(item)
输出:
[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 3, 4]
import itertools
for item in itertools.chain([1, 2, 3], ['a', 'b', 'c']):
print(item)
输出:
1
2
3
a
b
c
from itertools import compress
print(list(compress('ABCDEF', [1, 1, 0, 1, 0, 1])))
输出:
['A', 'B', 'D', 'F']
selectors默认为false:
from itertools import compress
print(list(compress('ABCDEF',[True])))
输出:
['A']
from itertools import dropwhile
print(list(dropwhile(lambda x: x < 5, [1, 3, 6, 2, 1])))
[6, 2, 1]
其中,lambda是用来创建匿名函数,对应的,用def创建的方法是有名称的。
from itertools import groupby
for key, value_iter in groupby('aaabbbaaccd'):
print(key, ':', list(value_iter))
输出:
a : ['a', 'a', 'a']
b : ['b', 'b', 'b']
a : ['a', 'a']
c : ['c', 'c']
d : ['d']
也可指定分组对象:
from itertools import groupby
data = ['a', 'gg', 'bb', 'ccc', 'dd', 'eee', 'f']
for key, value_iter in groupby(data, len):
print(key, ':', list(value_iter))
输出:
1 : ['a']
2 : ['gg', 'bb']
3 : ['ccc']
2 : ['dd']
3 : ['eee']
1 : ['f']
from itertools import islice
print(list(islice([10, 6, 2, 8, 1, 3, 9], 5)))
输出:
[10, 6, 2, 8, 1]
可指定开始索引和步长:
from itertools import count, islice
print(list(islice(count(), 3, 10, 2)))
输出:
[3, 5, 7, 9]
from itertools import tee
iter1, iter2, iter3 = tee('abcd', 3)
print(*iter1, list(iter2), list(iter3))
输出:
a b c d ['a', 'b', 'c', 'd'] ['a', 'b', 'c', 'd']
from itertools import takewhile
print(list(takewhile(lambda x: x < 5, [1, 3, 6, 2, 1])))
输出:
[1, 3]
例一:
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')
from itertools import product
c = product('ab', range(3))
print(list(c))
输出:
[('a', 0), ('a', 1), ('a', 2), ('b', 0), ('b', 1), ('b', 2)]
例三:
from itertools import product
c = product('aA', 'bB', '1')
print(list(c))
输出:
[('a', 'b', '1'), ('a', 'B', '1'), ('A', 'b', '1'), ('A', 'B', '1')]
from itertools import product
print(list(product('ABC', repeat=2)))#相当于print(list(product('ABC', 'ABC')))
输出:
[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('C', 'A'), ('C', 'B'), ('C', 'C')]
from itertools import permutations
print(list(permutations('ABC', 2)))
输出:
[('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
from itertools import combinations
print(list(combinations('ABC', 2)))
输出:
[('A', 'B'), ('A', 'C'), ('B', 'C')]
类似:combinations_with_replacement
from itertools import combinations_with_replacement
print(list(combinations_with_replacement('ABC', 2)))
输出:
[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]