Detailed Explanation of itertools Module in Python Functional Programming

  • 2021-11-30 00:42:35
  • OfStack

Directory container and iterative object count () function cycle function repeat function enumerate function, add sequence number accumulate function chain and groupby function zip_longest and ziptee function compress function islice, dropwhile, takewhile, filterfalse, filter summary

Containers and Iterable Objects

Before you start, add a few basic concepts. There are containers and iterable objects in Python

Container: A data structure used to store multiple elements, such as lists, tuples, dictionaries, collections, etc.; Iterable objects: An object that implements the __iter__ method is called an iterative object.

Iterators and generators are also derived from iterative objects:

Iterator: Objects that implement both __iter__ and __next__ methods are called iterators; Generator: All functions with yield keyword are generators.

This makes it clear that the scope of iterable objects is larger than that of containers. Moreover, the iterative object can only be used once, and the StopIteration exception will be prompted when the value is obtained after use.

In addition, there are one limitations to iterable objects:

You cannot use len function on iterative objects; next method can be used to deal with iterative objects, and containers can also be converted into iterators through iter function. The for statement automatically calls the container's iter function, so the container can also be iterated.

count () function

count function 1 is generally compared with range function. For example, range function needs to define the lower limit, upper limit and step size of the generation range, while count function is different, specifying the lower limit and step size, and the upper limit value does not need to be declared.

The function prototype is declared as follows


count(start=0, step=1) --> count object

The test code is as follows, which must add the judgment condition of jumping out of the loop, otherwise the code will run straight.


from itertools import count
a = count(5, 10)
for i in a:
    print(i)
    if i > 100:
        break

In addition, the count function also accepts non-integer parameters, so what is defined in the following code is also correct.


from itertools import count
a = count(0.5, 0.1)
for i in a:
    print(i)
    if i > 100:
        break

cycle function

Using the cycle function, you can loop a set of values, and the test code is as follows:


from itertools import cycle
x = cycle(' Dream eraser abcdf')
for i in range(5):
    print(next(x), end=" ")
print("\n")
print("*" * 100)
for i in range(5):
    print(next(x), end=" ")

The code outputs the following:

Dream eraser

****************************************************************************************************
a b c d f

You can see that the cycle function is very similar to the for loop.

repeat function

The repeat function is used to return a value repeatedly. The official description of the function is as follows:


class repeat(object):
    """
    repeat(object [,times]) -> create an iterator which returns the object
    for the specified number of times.  If not specified, returns the object
    endlessly.

Take 1 simple test to see the effect:


from itertools import repeat
x = repeat(' Eraser ')
for i in range(5):
    print(next(x), end=" ")
print("\n")
print("*" * 100)
for i in range(5):
    print(next(x), end=" ")

No matter how you look at this function, it doesn't seem to be of great use.

enumerate function, add sequence number

This function has been briefly introduced in the previous article, and it is in the __builtins__ package, so it will not be explained too much. The basic format is as follows:


enumerate(sequence, [start=0])

Where the start parameter is the subscript start position.

accumulate function

This function returns 1 iterable object based on the given function, and the default is cumulative effect, that is, the second parameter is operator. add, and the test code is as follows:


from itertools import accumulate
data = [1, 2, 3, 4, 5]
#  Calculate cumulative sum 
print(list(accumulate(data)))  # [1, 3, 6, 10, 15]

For the above code, it is modified to accumulate.


from itertools import accumulate
import operator
data = [1, 2, 3, 4, 5]
#  Calculating accumulation 
print(list(accumulate(data, operator.mul)))

In addition, the second parameter can also be functions such as max, min, etc., such as the following code:


from itertools import accumulate
data = [1, 4, 3, 2, 5]
print(list(accumulate(data, max)))

The code outputs the following, which actually compares any two values in data and leaves the largest value.

[1, 4, 4, 4, 5]

chain and groupby Functions

The chain function is used to combine multiple iterators into a single iterator, while groupby can divide one iterator into multiple sub-iterators.

First, show the application of groupby function under 1:


from itertools import count
a = count(5, 10)
for i in a:
    print(i)
    if i > 100:
        break
0

The output is as follows:

[('Oak', < itertools._grouper object at 0x0000000001DD9438 > ),
('Skin', < itertools._grouper object at 0x0000000001DD9278 > ),
('Wipe', < itertools._grouper object at 0x00000000021FF710 > )]

In order to use the groupby function, it is recommended to sort the original list first, because it is a bit like slice 1, and if you find any difference, you will separate an iterator.

The chain function is used to splice multiple iteration objects as follows:


from itertools import count
a = count(5, 10)
for i in a:
    print(i)
    if i > 100:
        break
1

zip_longest and zip

The zip function has been explained in a previous blog. The difference between zip_longest and zip is that zip returns the shortest sequence, while zip_longest returns the longest sequence.

The test code is as follows, and the results can be compared by yourself.


from itertools import count
a = count(5, 10)
for i in a:
    print(i)
    if i > 100:
        break
2

zip_logest If it encounters a sequence of length less than 1, the missing part will fill None.

tee function

The tee function can clone iterative objects and produce multiple generators, each of which can produce individual elements of the input.


from itertools import tee
a = list(tee(' Eraser '))
print(a)

compress function

This function uses the ** predicate (NO, True/False) ** to determine the trade-off of an element. The simplest code is as follows:


from itertools import count
a = count(5, 10)
for i in a:
    print(i)
    if i > 100:
        break
4

islice, dropwhile, takewhile, filterfalse, filter

Each of these functions takes a subset from the input iterable object without modifying the element itself.

This section only lists the prototype declarations of each function, and the specific usage can be directly referred to.


from itertools import count
a = count(5, 10)
for i in a:
    print(i)
    if i > 100:
        break
5

Among them, only the parameters in filterfalse are function first and sequence second.

The test code is as follows, especially noting that the first parameter is callable, that is, a function.


from itertools import count
a = count(5, 10)
for i in a:
    print(i)
    if i > 100:
        break
6

Summarize

The above content is the whole content of this article. When using infinite iterator functions count, cycle and repeat, 1 must pay attention even if it stops.

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: