SoFunction
Updated on 2024-10-30

Python Programming itertools Module Handles Iterable Collection Related Functions

Containers and Iterable Objects

Before we get started, let's add some basic concepts. In Python, there are containers and iterable objects.

  • Container: A data structure used to store multiple elements, such as lists, tuples, dictionaries, collections, and so on;
  • Iterable objects: objects that implement the__iter__ The object of a method is called an iterable object.

Iterators and generators are also derived from iterable objects:

  • Iterators: both implement the__iter__The same has been achieved for the__next__ The object of the method is called an iterator;
  • Generator: withyield Functions with keywords are generators.

This makes it clearer that the scope of the iterable object is larger than the container. Also, iterable objects can only be used once, and getting the value again will prompt theStopIteration Exception.

Beyond that, iterable objects have some limitations:

  • Cannot be used on iterable objectslen function;
  • It is possible to usenext method handles iterable objects, and containers can also be used to handle iterable objects via theiter function into an iterator;
  • The for statement automatically calls the container'siter function, so the container can also be iterated through the loop.

count() function

count Functions are generally associated withrange Comparative learning of functions, e.g.range function needs to define the lower limit of the generated range, the upper limit with an optional step size, and thecount function is different, specify the lower limit and step size, 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 be added to jump out of the loop conditions, otherwise the code will keep running.

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

Beyond that.count The function also takes non-integer arguments, so the definition 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

expense or outlaycycle function can loop through a set of values, the test code is shown below:

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 Rubber Rub
****************************************************************************************************
a b c d f

can be seencycle Comparison of functions withfor The loop is very similar.

repeat function

repeat function is used to return a value repeatedly, the official description of the function is shown below:

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.

Perform a simple test to see the results:

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=" ")

Any way you look at this function, it doesn't seem to be very useful.

enumerate function, add serial number

This function has been briefly described in a previous article, and the function has been__builtins__ package, so I won't go into too much detail

The basic format is shown below:

enumerate(sequence, [start=0])

included among thesestart The parameter is the subscript start position.

accumulate function

This function returns an iterable object based on the given function, which by default is cumulative in effect, i.e. the second parameter isThe test code is as follows:

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

For the above code, modify to cumulative.

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

In addition to this, the second parameter can bemaxmin 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 is actually a combination ofdata Any two values inside are compared and the largest value is left.

[1, 4, 4, 4, 5]

chain and groupby functions

chain function is used to combine multiple iterators into a single iterator, while thegroupby It is possible to divide an iterator into multiple subiterators.

Let's start by showinggroupby Application of functions:

from itertools import groupby
a = list(groupby('Eraser Skin Eraser'))
print(a)

The output is shown below:

[('Quercus serrata', <itertools._grouper object at 0x0000000001DD9438>),
('pico- (one trillionth)', <itertools._grouper object at 0x0000000001DD9278>),
('rubbing (brush stroke in painting)', <itertools._grouper object at 0x00000000021FF710>)]

In order to usegroupby function, it is recommended that the original list be sorted first, as it is a bit like slicing, finding different ones and splitting an iterator.

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

from itertools import groupby, chain
a = list(chain('ABC', 'AAA', range(1,3)))
print(a)

zip_longest vs.

zip function has been explained in a previous blog post.zip_longest together withzip The difference is that thezip The result returned is based on the shortest sequence, and thezip_longest Whichever is the longest.

The test code is below, just compare the results yourself.

from itertools import zip_longest
a = list(zip('ABC', range(5), [10, 20, 30, 40]))
print(a)
a = list(zip_longest('ABC', range(5), [10, 20, 30, 40]))
print(a)

zip_logest If a sequence of inconsistent lengths is encountered, the missing portion is padded with theNone

tee function

tee Functions can clone iterable objects to 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

The function determines the trade-off problem for an element by means of a predicate (whether, True/False), the simplest code of which is shown below:

from itertools import compress
a = list(compress('Eraser', (0, 1, 1)))
print(a)

islice、dropwhile、takewhile、filterfalse、filter

Each of these functions takes a subset from the input iterable and does not modify the elements themselves.

This section only lists the prototype declaration of each function, the specific use of direct reference to use.

islice(iterable, stop) --> islice object
islice(iterable, start, stop[, step]) --> islice object
dropwhile(predicate, iterable) --> dropwhile object
takewhile(predicate, iterable) --> takewhile object
filterfalse(function or None, sequence) --> filterfalse object

Of these, onlyfilterfalse The arguments in are the function first and the sequence second.

The test code is as follows, noting in particular that the first parameter iscallable That is, functions.

from itertools import islice, dropwhile, takewhile, filterfalse
a = list(filterfalse(lambda x: x in ["Pi.", "Rub."], 'Eraser'))
print(a)

Write it on the back.

The above is what this article is all about, in using the infinite iterator functioncountcyclerepeat It is important to be careful to stop even when the

The above is Python programming itertools module to deal with iteratable sets of related functions in detail, more information about Python programming itertools module to deal with iteratable sets of functions, please pay attention to my other related articles!