itertools 模块提供了许多高效的迭代器工具:
import itertools
# count(start, step):无限计数
counter = itertools.count(10, 2)
print(next(counter)) # 10
print(next(counter)) # 12
print(next(counter)) # 14
# cycle(iterable):无限循环
cy = itertools.cycle(["A", "B", "C"])
print(next(cy)) # A
print(next(cy)) # B
print(next(cy)) # C
print(next(cy)) # A(重新开始)
# repeat(value, times):重复值
times_three = list(itertools.repeat("x", 3))
print(times_three) # ['x', 'x', 'x']
# chain(*iterables):连接多个可迭代对象
combined = list(itertools.chain([1, 2], [3, 4], [5, 6]))
print(combined) # [1, 2, 3, 4, 5, 6]
# islice(iterable, start, stop, step):切片(支持无限迭代器)
first_five = list(itertools.islice(itertools.count(), 5))
print(first_five) # [0, 1, 2, 3, 4]
# permutations(iterable, r):排列
perms = list(itertools.permutations([1, 2, 3], 2))
print(perms) # [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
# combinations(iterable, r):组合
combs = list(itertools.combinations([1, 2, 3, 4], 2))
print(combs) # [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]




