About Python Built in Library itertools
- 2021-12-04 19:11:31
- OfStack
Foreword:
Recently, there are not many things. I want to write some technical articles to share with you. At the same time, I also sort out the knowledge I have received in a fragmented way for a period of time. The so-called writing clearly can make it clear, and speaking clearly can make it clear. This is the reason.
Many people are committed to putting
Python
The code is written more
Pythonic
1 to be more spec-compliant and easy to read, 2 to 1
Pythonic
The code of is also more efficient in execution. Let me introduce you to you today
Python
System library of
itertools
.
1. itertools Library
Iterator (generator) in the
Python
Is a very common and easy-to-use data structure. Compared with the list (list), the biggest advantage of iterator is to delay calculation and use it on demand, thus improving the development experience and running efficiency, so that in
Python 3
map, filter and so on return iterators instead of lists.
Having said that, the iterators used by everyone at ordinary times are probably only range, and it is a bit more to convert list objects into iterator objects through iter function. At this time, our protagonist today
itertools
It's time to play.
2. Use itertools
itertools
Most of the functions in return to various iterator objects, many of which we usually have to write a lot of code to achieve, but the running efficiency is lower, after all, people are system libraries.
3. itertools. accumulate
Simply put, it is accumulation.
>>> import itertools
>>> x = itertools.accumulate(range(10))
>>> print(list(x))
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45]
4. itertools. chain
Connecting 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
Finds all combinations of the specified number of elements in the list or generator that do not repeat
>>> x = itertools.combinations(range(4), 3)
>>> print(list(x))
[(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]
5. itertools.combinations_with_replacement
Allow duplicate combinations of elements
>>> x = itertools.combinations_with_replacement('ABC', 2)
>>> print(list(x))
[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]
6. itertools. compress
Filter elements by truth table
>>> x = itertools.compress(range(5), (True, False, True, True, False))
>>> print(list(x))
[0, 2, 3]
7. itertools. count
Is a counter, which 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]
8. itertools. cycle
Loop the specified list and iterator
>>> x = itertools.cycle('ABC')
>>> print(list(itertools.islice(x, 0, 10, 1)))
['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A']
9. 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]
10. itertools. filterfalse
Retain the corresponding element whose truth value is False
>>> x = itertools.filterfalse(lambda e: e < 5, (1, 5, 3, 6, 9, 4))
>>> print(list(x))
[5, 6, 9]
11. itertools. groupby
Group 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]
12. itertools. islice
The function used above slices the iterator
>>> 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
0
13. itertools. permutations
All permutations (order dependent) that produce a specified number of elements
>>> 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
1
14. itertools. product
Generate the product of multiple lists and 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
2
15. 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]
16. itertools. starmap
Similar to map
>>> 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
4
17. itertools. takewhile
And
dropwhile
Instead, keep the element until the truth function value is false.
>>> 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
5
18. itertools. tee
I don't know much about this function either. It seems to generate a specified number of 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
6
19. itertools.zip_longest
Similar to zip, but the longer list and iterator length prevail
>>> 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
7
Conclusion: