itertools库中的函数大多是返回各种迭代器对象
itertools.accumulate
分项累加,返回迭代器
>>> import itertools
>>> x = itertools.accumulate(range(9))
>>> x
>>> list(x)
[0, 1, 3, 6, 10, 15, 21, 28, 36]
itertools.chain()
连接列表或者迭代器,返回迭代器
>>> x = itertools.chain(range(3),range(5),[2,4,5,8])
>>> list(x)
[0, 1, 2, 0, 1, 2, 3, 4, 2, 4, 5, 8]
>>> x
itertools.combinations
求列表或生成器中指定数目的元素不重复的所有组合
如果对元素唯一性有要求,需要先做唯一化处理
>>> x = itertools.combinations([2,3,4,4],3)
>>> list(x)
[(2, 3, 4), (2, 3, 4), (2, 4, 4), (3, 4, 4)]
>>> x = itertools.combinations(range(4),3)
>>> list(x)
[(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]
itertools.combinations_with_replacement
允许重复元素的组合
>>> x = itertools.combinations_with_replacement("ABC",2)
>>> list(x)
[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]
itertools.compress
按照真值表筛选元素
>>> x = itertools.compress(range(3),(1,0,1))
>>> list(x)
[0, 2]
>>> x = itertools.compress(range(3),(True,False,True))
>>> list(x)
[0, 2]
itertools.islice
对迭代器进行切片
>>> x = itertools.islice(range(12),0,9,2)
>>> list(x)
[0, 2, 4, 6, 8]
itertools.count
就是一个计数器,可以指定起始位置和步长
>>> x = itertools.count(start=20,step=-1)
>>> list(itertools.islice(x,0,15,1))
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6]
itertools.cycle
循环指定的列表和迭代器
>>> x = itertools.cycle('abc')
>>> list(itertools.islice(x,0,5,1))
['a', 'b', 'c', 'a', 'b']
>>> x = itertools.cycle([1,2,3])
>>> list(itertools.islice(x,0,5,1))
[1, 2, 3, 1, 2]
>>> x = itertools.cycle(range(3))
>>> list(itertools.islice(x,0,5,1))
[0, 1, 2, 0, 1]
itertools.dropwhile
按照真值函数丢弃掉列表和迭代器前面的元素
>>> x = itertools.dropwhile(lambda t:t<5,range(3,8))
>>> list(x)
[5, 6, 7]
itertools.takewhile
与dropwhile相反,保留元素直至真值函数值为假。
>>> x = itertools.takewhile(lambda t:t<5,range(3,8))
>>> list(x)
[3, 4]
itertools.filterfalse
保留对应真值为False的元素
>>> x = itertools.filterfalse(lambda t:t<5,range(3,8))
>>> list(x)
[5, 6, 7]
itertools.groupby
按照分组函数的值对元素进行分组
>>> x = itertools.groupby(range(20),lambda x:x<5 or x >12)
>>> for condition,numbers in x:
... print(condition,list(numbers))
...
True [0, 1, 2, 3, 4]
False [5, 6, 7, 8, 9, 10, 11, 12]
True [13, 14, 15, 16, 17, 18, 19]
itertools.permutations
产生指定数目的元素的所有排列(顺序有关)
>>> x = itertools.permutations(range(4),3)
>>> list(x)
[(0, 1, 2), (0, 1, 3), (0, 2, 1), (0, 2, 3), (0, 3, 1), (0, 3, 2), (1, 0, 2), (1, 0, 3), (1, 2, 0), (1, 2, 3), (1, 3, 0), (1, 3, 2), (2, 0, 1), (2, 0, 3), (2, 1, 0), (2, 1, 3), (2, 3, 0), (2, 3, 1), (3, 0, 1), (3, 0, 2), (3, 1, 0), (3, 1, 2), (3, 2, 0), (3, 2, 1)]
itertools.product
产生多个列表和迭代器的(积)
>>> x = itertools.product('ABC',range(5))
>>> list(x)
[('A', 0), ('A', 1), ('A', 2), ('A', 3), ('A', 4), ('B', 0), ('B', 1), ('B', 2), ('B', 3), ('B', 4), ('C', 0), ('C', 1), ('C', 2), ('C', 3), ('C', 4)]
itertools.repeat
简单的生成一个拥有指定数目元素的迭代器
>>> x = itertools.repeat(0,5)
>>> list(x)
[0, 0, 0, 0, 0]
>>> x = itertools.repeat(2,5)
>>> list(x)
[2, 2, 2, 2, 2]
itertools.starmap
类似map
>>> x = itertools.starmap(str.islower,'sdSdfDdDWsdf')
>>> list(x)
[True, True, False, True, True, False, True, False, False, True, True, True]
itertools.tee(iter or list,num)
>>> x = itertools.tee(range(5),3)
>>> type(x)
>>> list(x[0])
[0, 1, 2, 3, 4]
>>> list(x[1])
[0, 1, 2, 3, 4]
>>> list(x[2])
[0, 1, 2, 3, 4]
>>> x
(
itertools.zip_longest
类似于zip,不过以较长的列表和迭代器的长度为准
>>> x = itertools.zip_longest(range(5),[1,2,4])
>>> list(x)
[(0, 1), (1, 2), (2, 4), (3, None), (4, None)]
参考地址:https://www.jb51.net/article/123094.htm