Python built-in Library: itertools

Source: Yixian

01 Preface

Many people are committed to writing Python code more python, which is more standard and easy to read, and the general Python code is also more efficient in execution. Today, let's introduce the python system library itertools.

02 itertools Library

Iterator (generator) is a very common and easy-to-use data structure in Python. Compared with list, the biggest advantage of iterator is to delay calculation and use it on demand, so as to improve the development experience and operation efficiency, so that in Python 3, map,filter and other operations return iterators instead of lists.

Having said that, the iterator we usually use is probably only range, and it's a little superfluous to convert the list object into iterator object through iter function. At this time, our protagonist itertools will play today.

03 using itertools

Most of the functions in itertools return all kinds of iterator objects. We usually have to write a lot of code to achieve the function of many of them, but the efficiency is lower. After all, people are system libraries.

itertools.accumulate

In short, it is accumulation.

>>> import itertools
>>> x = itertools.accumulate(range(10))
>>> print(list(x))
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45]

itertools.chain

Connect multiple lists or iterators.

>>> x = itertools.chain(range(3), range(4), [3,2,1])
>>> print(list(x))
[0, 1, 2, 0, 1, 2, 3, 3, 2, 1]

itertools. Combines all combinations that do not duplicate a specified number of elements in a list or generator

>>> x = itertools.combinations(range(4), 3)
>>> print(list(x))
[(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]

itertools.combinations_with_replacement allows the combination of repeating elements

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

itertools.compress filters elements by truth table

>>> x = itertools.compress(range(5), (True, False, True, True, False))
>>> print(list(x))
[0, 2, 3]

itertools.count is a counter that can specify the starting position and step size

>>> 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. The list and iterator specified by the cycle loop

>>> x = itertools.cycle('ABC')
>>> print(list(itertools.islice(x, 0, 10, 1)))
['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A']

itertools.dropwhile discards the elements in front of the list and iterator according to the truth function

>>> x = itertools.dropwhile(lambda e: e < 5, range(10))
>>> print(list(x))
[5, 6, 7, 8, 9]

itertools.filterfalse preserves elements whose corresponding true value is False

>>> x = itertools.filterfalse(lambda e: e < 5, (1, 5, 3, 6, 9, 4))
>>> print(list(x))
[5, 6, 9]

itertools. Group by groups elements according to the value of the grouping function

>>> 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 slices the iterator using the functions used above

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

itertools.permutations produces all permutations of a specified number of elements (order dependent)

>>> x = itertools.permutations(range(4), 3)
>>> print(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 produces multiple lists and iterators

>>> x = itertools.product('ABC', range(3))
>>>
>>> print(list(x))
[('A', 0), ('A', 1), ('A', 2), ('B', 0), ('B', 1), ('B', 2), ('C', 0), ('C', 1), ('C', 2)]

itertools.repeat simply generates an iterator with a specified number of elements

>>> x = itertools.repeat(0, 5)
>>> print(list(x))
[0, 0, 0, 0, 0]

itertools.starmap is similar to map

>>> x = itertools.starmap(str.islower, 'aBCDefGhI')
>>> print(list(x))
[True, False, False, False, True, True, False, True, False]

itertools. In contrast to dropwhile, takeWhile keeps the element until the value of the truth function is false.

>>> x = itertools.takewhile(lambda e: e < 5, range(10))
>>> print(list(x))
[0, 1, 2, 3, 4]

itertools. It seems that I know the purpose of generating iterators

>>> x = itertools.tee(range(10), 2)
>>> for letters in x:
...     print(list(letters))
...
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

itertools.zip_longest is similar to zip, but the length of the longer list and iterator prevail

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

04 the conclusion is probably summarized here, but to be honest, Python's various language features and libraries still need to be used more in order to be proficient, and finally reach the level of handy. Pretending to force is to enter the Tao from the art-  END -

contrast Excel The cumulative sales volume of series books reached 15 w Book, so that you can easily master the skills of data analysis. You can search the title of the book in the whole network to understand:

Added by nuxy on Thu, 03 Mar 2022 13:43:06 +0200