Article From:https://www.cnblogs.com/woshijiuke/p/9122288.html

Preface

Recent things are not many, want to write some technical articles to share, but also to their own time to the fragmentation of the knowledge to be combed, so called to write clearly can be clear, to clear to clear, that is the truth.

Many people are committed to writing Python code more Pythonic, which is more standard and easy to read, and second, the code of the general Pythonic is more efficient in execution. Today we will first introduce the system library itertools of Python.

itertoolslibrary

An iterator (generator) is a very common and well used data structure in Python, and the greatest advantage of the iterator is to delay computing, use it on demand, so as to improve development experience and efficiency, so that map, filt in Python 3, compared to the list (list).Er and other operations return no longer lists but iterators.

In that case, the iterator that you usually use is about range, and it’s a little bit more to convert the list object into an iterator object through the ITER function, and the leading role of today’s itertools should be on the field.

Using itertools

itertoolsMost of the functions are returned to a variety of iterator objects, and many of the functions we usually have to write a lot of code to achieve, but the efficiency of the work is lower, after all, the family is the system library.

itertools.accumulate

In simple terms, it’s accumulating.

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

Find all combinations of specified numbers 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

Allowing the combination of repeated 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

Filter elements according to the truth table

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

itertools.count

It is a counter that specifies the starting position and step length.

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

Loop specified lists and iterators

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

itertools.dropwhile

Discard 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

Retain the element that corresponds to the true value of False

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

itertools.groupby

Groups of elements are grouped according to the value of the group 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

The functions used above are used to slice iterators.

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

itertools.permutations

All permutations of a specified number of elements (sequence related).

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

Producing multiple lists and iterators (products)

>>> 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 generate an iterator with a specified number of elements.

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

itertools.starmap

Similar to map

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

itertools.takewhile

In contrast to dropwhile, retain the element until the true value function is false.

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

itertools.tee

I don’t quite understand this function. It seems to be generating a specified number of 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

Similar to zip, however, the longer list and iterator length is.

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

epilogue

It will probably be summed up here, but to be honest, the language features and libraries of Python should be more used to be proficient, and finally to the extent that they come in.

 

This article is supported by the Python College of mago education. If you have any problems in learning Python, please contact me by QQ:2282011279 or telephone: 15652199186.

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *