参考
>>> import itertools
>>> import operator
# accumulate实际就是和上一个元素进行某种运算
>>> data = [i for i in range(1,10)] #[1, 2, 3, 4, 5, 6, 7, 8, 9]
# 类乘
>>> list(itertools.accumulate(data,operator.mul)) #[1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
# 累加
>>> list(itertools.accumulate(data,operator.add)) #[1, 3, 6, 10, 15, 21, 28, 36, 45]
# 求最大值
>>> list(itertools.accumulate(data,max)) #[1, 2, 3, 4, 5, 6, 7, 8, 9]
# 和lambda结合
>>> list(itertools.accumulate(data, lambda x, y: x*2 + y)) [1, 4, 11, 26, 57, 120, 247, 502, 1013]
>>> x = itertools.chain(range(3), range(4), [3,2,1])
>>> list(x) # [0, 1, 2, 0, 1, 2, 3, 3, 2, 1]
>>> x = itertools.combinations(range(4), 3)
>>> list(x) # [(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]
>>> x = itertools.permutations([0,1,1], 3)
>>> list(x)
[(0, 1, 1), (0, 1, 1), (1, 0, 1), (1, 1, 0), (1, 0, 1), (1, 1, 0)]
>>> x = itertools.combinations_with_replacement('ABC', 2)
>>> print(list(x))
[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]
>>> s = "ABCDEFGHIJK"
>>> list(itertools.compress(s,[1,0,1,0,1,1]))
['A', 'C', 'E', 'F']
>>> x = itertools.filterfalse(lambda e: e < 5, (1, 5, 3, 6, 9, 4))
>>> print(list(x))
[5, 6, 9]
>>> x = itertools.count(start=20, step=-1)
>>> print(list(itertools.islice(x, 0, 10, 1)))
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11]
>>> x = itertools.cycle('ABC')
>>> print(list(itertools.islice(x, 0, 10, 1)))
['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A']
>>> x = itertools.dropwhile(lambda e: e < 5, range(10))
>>> print(list(x))
[5, 6, 7, 8, 9]
>>> y = itertools.takewhile(lambda x: x<5, [1,4,6,4,1])
>>> list(y)
[1, 4]
>>> x = itertools.groupby(range(10), lambda x: x < 5 or x > 8)
>>> for condition, numbers in x:
print(condition, list(numbers))
True [0, 1, 2, 3, 4]
False [5, 6, 7, 8]
True [9]
>>> x = itertools.islice(range(10), 0, 9, 2) # 注意是大小不是个数
>list(x)
>[0, 2, 4, 6, 8]
>>> list(itertools.product('ABC',repeat =1))
[('A',), ('B',), ('C',)]
>>> list(itertools.product('ABC',repeat =2))
[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('C', 'A'), ('C', 'B'), ('C', 'C')]
>>> list(itertools.product('ABC',repeat =3))
[('A', 'A', 'A'), ('A', 'A', 'B'), ('A', 'A', 'C'), ('A', 'B', 'A'), ('A', 'B', 'B'), ('A', 'B', 'C'), ('A', 'C', 'A'), ('A', 'C', 'B'), ('A', 'C', 'C'), ('B', 'A', 'A'), ('B', 'A', 'B'), ('B', 'A', 'C'), ('B', 'B', 'A'), ('B', 'B', 'B'), ('B', 'B', 'C'), ('B', 'C', 'A'), ('B', 'C', 'B'), ('B', 'C', 'C'), ('C', 'A', 'A'), ('C', 'A', 'B'), ('C', 'A', 'C'), ('C', 'B', 'A'), ('C', 'B', 'B'), ('C', 'B', 'C'), ('C', 'C', 'A'), ('C', 'C', 'B'), ('C', 'C', 'C')]
>>> list(map(pow, range(10), itertools.repeat(2)))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> list(map(pow, range(10), itertools.repeat(3)))
[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
>>> x = itertools.starmap(str.islower, 'aBCDefGhI')
>>> print(list(x))
[True, False, False, False, True, True, False, True, False]
>>> x = itertools.zip_longest(range(3), range(5))
>>> y = zip(range(3), range(5))
>>> print(list(x))
[(0, 0), (1, 1), (2, 2), (None, 3), (None, 4)]
>>> print(list(y))
[(0, 0), (1, 1), (2, 2)]