Summary of itertools Module in python
- 2021-12-12 05:13:20
- OfStack
The built-in itertools module of Python contains a series of functions or classes used to generate different types of iterators. The return of these functions is an iterator. We can traverse the value through for loop or use next () to take the value.
The itertools module provides the following types of iterator functions:
Infinite iterator: Generate an infinite sequence, such as natural number sequence 1, 2, 3, 4,...; Finite iterator: receiving one or more sequences (sequence) as parameters, combining, grouping and filtering; Combination generator: the arrangement and combination of sequences, and the Cartesian product of sequences.itertools
Creating a standard library of circulators under efficient circulation
Infinite itertools, Infinite Iterator
itertools.count(start=0, step=10)
By default, it returns 1 natural number iterator starting from 0 and +10 in turn. If you don't stop it, it will run straight at 1.
You can specify the starting position with start, and step is the step size.
import itertools
c = itertools.count(start=0, step=1)
for i in c:
print(i)
# 0
# 10
# 20
# 30
# 40
# 50
# ...
itertools.cycle(iterable)
Pass in an iterable object, and then iterate indefinitely.
import itertools
# itertools.count()
l = [1,2,3,4,5]
c = itertools.cycle(l)
for i in c:
print(i)
# 1
# 2
# 3
# 4
# 5
# 1
# 2
# 3
# 4
# 5
# ...
itertools.repeat(p_object, times=None)
Iterate one object p_object repeatedly. If times is not specified, it will be iterated countless times. You can also specify the number of iterations through times parameter.
import itertools
# itertools.count()
l = [1,2,3,4,5]
c = itertools.repeat(l, 5)
for i in c:
print(i)
# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]
Iterators terminating on the shortest input sequence
itertools.accumulate(iterable, func)
Returns the cumulative value of a sequence or other binary function.
import itertools
# itertools.count()
l = [1,2,3,4,5]
c = itertools.accumulate(l)
print(c)
for i in c:
print(i)
# 1
# 3
# 6
# 10
# 15
accumulate () still returns an iterator, passing an list, and when you walk through the print in the for loop, you find that it does an accumulation operation. (Iterating the first number is the sum of the first one, iterating to the second number is the sum of the first two numbers, and so on)
It also supports list, tuple, str, set and dict when doing incremental operation
If the dict object is passed in, key iterating over dict will be accumulated:
import itertools
# itertools.count()
d = {'a': 1, 'b': 2, 'c': 3}
c = itertools.accumulate(d)
print(c)
for i in c:
print(i)
# <itertools.accumulate object at 0x000001F7A0A90E48>
# a
# ab
# abc
itertools.chain(*iterables)
Parameters in the chain () method can pass in multiple sequences, and as long as they are sequences, the data type of the sequence is not limited.
For example, iterate three sequences of list, tuple and str
import itertools
l = [1, 2, 3, 4, 5]
t = (1, 2, 3, 4, 5)
s = 'abcdefg'
c = itertools.chain(l, t, s)
print(c)
for i in c:
print(i)
# <itertools.chain object at 0x0000026E64801448>
# 1
# 2
# 3
# 4
# 5
# 1
# 2
# 3
# 4
# 5
# a
# b
# c
# d
# e
# f
# g
itertools Cartesian product
import itertools
d = [
[{"a": 1}, {"b": 2}], [{"c": 3}, {"d": 4}, {"e": 5}], [{"f": 6}, {"g": 7}]
]
print(*d)
for i in itertools.product(*d):
print(i)
# [{'a': 1}, {'b': 2}] [{'c': 3}, {'d': 4}, {'e': 5}] [{'f': 6}, {'g': 7}]
# ({'a': 1}, {'c': 3}, {'f': 6})
# ({'a': 1}, {'c': 3}, {'g': 7})
# ({'a': 1}, {'d': 4}, {'f': 6})
# ({'a': 1}, {'d': 4}, {'g': 7})
# ({'a': 1}, {'e': 5}, {'f': 6})
# ({'a': 1}, {'e': 5}, {'g': 7})
# ({'b': 2}, {'c': 3}, {'f': 6})
# ({'b': 2}, {'c': 3}, {'g': 7})
# ({'b': 2}, {'d': 4}, {'f': 6})
# ({'b': 2}, {'d': 4}, {'g': 7})
# ({'b': 2}, {'e': 5}, {'f': 6})
# ({'b': 2}, {'e': 5}, {'g': 7})