In [1]: import itertools
In [2]: dir(itertools)
Out[2]:
['__doc__',
'__file__',
'__name__',
'__package__',
'chain',
'combinations',
'combinations_with_replacement',
'compress',
'count',
'cycle',
'dropwhile',
'groupby',
'ifilter',
'ifilterfalse',
'imap',
'islice',
'izip',
'izip_longest',
'permutations',
'product',
'repeat',
'starmap',
'takewhile',
'tee']
- itertools.chain
# 连接多个列表或生成器
In [7]: list(itertools.chain(range(3), range(3,6), [6,7,8,9]))
Out[7]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- itertools.combinations
# 求列表或生成器中指定数目的元素不重复的所有组合
In [4]: list(itertools.combinations(range(5), 3))
Out[4]:
[(0, 1, 2),
(0, 1, 3),
(0, 1, 4),
(0, 2, 3),
(0, 2, 4),
(0, 3, 4),
(1, 2, 3),
(1, 2, 4),
(1, 3, 4),
(2, 3, 4)]