python itertools库

参考

itertools.accumulate(iterable[, func ])
>>> 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]
itertools.chain 连接多个列表或者迭代器
>>> x = itertools.chain(range(3), range(4), [3,2,1])
>>> list(x) # [0, 1, 2, 0, 1, 2, 3, 3, 2, 1]
itertools.combinations 求列表或生成器中指定数目的元素不重复的所有组合
>>> x = itertools.combinations(range(4), 3)
>>> list(x) # [(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]
itertools.permutations(iterable, r=None),输出输入序列的全排列
The number of items returned is n! / (n-r)! when 0 <= r <= n or zero when r > n.根据序列位置进行全排列,而不是值,所以如果输入序列有重复值,输出亦会有
>>> 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)]
itertools.combinations_with_replacement(iterable, r) 允许重复元素的组合
>>> x = itertools.combinations_with_replacement('ABC', 2)
>>> print(list(x))
[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]
itertools.compress(data, selectors) 按照真值表筛选元素
>>> s = "ABCDEFGHIJK"
>>> list(itertools.compress(s,[1,0,1,0,1,1]))
['A', 'C', 'E', 'F']
itertools.filterfalse 保留对应真值为False的元素
>>> x = itertools.filterfalse(lambda e: e < 5, (1, 5, 3, 6, 9, 4))
>>> print(list(x))
[5, 6, 9]
itertools.count(start=0, step=1)
>>> 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]
itertools.cycle(iterable) 循环指定的列表和迭代器
>>> x = itertools.cycle('ABC')
>>> print(list(itertools.islice(x, 0, 10, 1)))
['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A']
itertools.dropwhile(predicate, iterable) 根据真值表舍弃第一个不满足条件前面的值
>>> x = itertools.dropwhile(lambda e: e < 5, range(10))
>>> print(list(x))
[5, 6, 7, 8, 9]

itertools.takewhile(predicate, iterable) 保留序列元素直到条件不满足
>>> y = itertools.takewhile(lambda x: x<5, [1,4,6,4,1])
>>> list(y)
[1, 4]
itertools.groupby(iterable, key=None) 按照分组函数的值对元素进行分组
>>> 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]
itertools.islice(iterable, stop)
itertools.islice(iterable, start, stop[, step ])
>>> x = itertools.islice(range(10), 0, 9, 2) # 注意是大小不是个数
>list(x)
>[0, 2, 4, 6, 8]
itertools.product(*iterables, repeat=1) Cartesian product of input iterables.
product(A, B) returns the same as ((x,y) for x in A for y in B)
>>> 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')]
itertools.repeat(object[, times ])
>>> 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]
itertools.starmap(function, iterable)
map() and starmap()的区别类似于function(a,b) and function(*c).
>>> x = itertools.starmap(str.islower, 'aBCDefGhI')
>>> print(list(x))
[True, False, False, False, True, True, False, True, False]
itertools.zip_longest(*iterables, fillvalue=None)
>>> 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)]

你可能感兴趣的:(python)