itertools library use

Most of the library's function is to return itertools various iterations object

itertools.accumulate  

Sub accumulated, returns an iterator

>>> import itertools
>>> x = itertools.accumulate(range(9))
>>> x
<itertools.accumulate object at 0x02E22B98>
>>> list(x)
[0, 1, 3, 6, 10, 15, 21, 28, 36]

itertools.chain()

Connection list or iterator returns an iterator

>>> 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.chain object at 0x02E10A50>

itertools.combinations

Builder or list of requirements specified number of elements will not be repeated for all combinations

If required by the uniqueness of the elements, the only treatment need to do first

>>> 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

Repeating elements in combination allows

>>> x = itertools.combinations_with_replacement("ABC",2)
>>> list(x)
[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]

itertools.compress

Filter element according to truth table

>>> 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

Iterator sliced

>>> x = itertools.islice(range(12),0,9,2)
>>> list(x)
[0, 2, 4, 6, 8]

itertools.count

It is a counter, and can specify the starting position of the step

>>> 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

Cycle specified list and iterators

>>> 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

Discarded and the preceding list element according iterator truth functions

>>> x = itertools.dropwhile(lambda t:t<5,range(3,8))
>>> list(x)
[5, 6, 7]

itertools.takewhile

In contrast with dropwhile retention elements function until the true value is false.

>>> x = itertools.takewhile(lambda t:t<5,range(3,8))
>>> list(x)
[3, 4]

itertools.filterfalse

Retention elements corresponding to the true value of False

>>> x = itertools.filterfalse(lambda t:t<5,range(3,8))
>>> list(x)
[5, 6, 7]

itertools.groupby

The elements are grouped by the function value of the packet

>>> 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

Generating a specified number of all permutations of elements (order dependent)

>>> 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

Generating a plurality of lists and iterators (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

Simple iterator has generated a specified number of elements

>>> 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

Similar 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)
<class 'tuple'>
>>> 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._tee object at 0x02E27918>, <itertools._tee object at 0x02E27968>, <itertools._tee object at 0x02E27990>)

itertools.zip_longest

Similar to zip, but the longer the length of the list and iterators prevail

>>> x = itertools.zip_longest(range(5),[1,2,4])
>>> list(x)
[(0, 1), (1, 2), (2, 4), (3, None), (4, None)]

Reference Address: https://www.jb51.net/article/123094.htm

Guess you like

Origin www.cnblogs.com/hongliangzhao/p/12669055.html