Python Programming itertools Module Handles Iterable Set Correlation Functions

  • 2021-11-30 00:58:36
  • OfStack

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 object: implements the __iter__ Method is called an iterative object.

Iterators and generators are also derived from iterative objects:

Iterator: It implements the __iter__ , also realized __next__ The object of the method is called an iterator; Generator: Has a yield Keyword is a generator.

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

In addition, there are one limitations to iterable objects:

You cannot use the len Function; You can use the next Method handles iterative objects, and containers can also use the iter Function conversion to iterator; The for statement automatically calls the container's iter Function, so the container can also be iterated by loop.

count () function

count General and of function 1 __iter__0 Function comparison learning, such as __iter__0 Function needs to define the lower limit, upper limit and step size of the build range, while count Function, specify 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, 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

Use cycle Function 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 cycle Function and for Loop is very similar.

repeat function

repeat The 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 was briefly introduced in the previous article, and it is used in the __builtins__ Package, so don't explain too much

The basic format is as follows:

enumerate(sequence, [start=0])

Among them 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 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 max , min And so on, 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 the data Any two values in it are compared, and then 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, and groupby One iterator can be divided into multiple sub-iterators.

First show 1 groupby Application of function:


from itertools import groupby
a = list(groupby(' Rubber skin wipe '))
print(a)

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 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 1 iterator.

chain The function is used to splice multiple iteration objects


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

zip_longest and zip

zip Function has been explained in a previous blog, yield0 And zip The difference is that, zip The result returned is based on the shortest sequence, and yield0 Whichever is the longest.

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


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

zip_logest If a sequence of length less than 1 is encountered, the missing part will be filled None .

tee function

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


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

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(0.5, 0.1)
for i in a:
    print(i)
    if i > 100:
        break
2

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.


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 which only filterfalse The parameter in is that the function comes first and the sequence comes last.

The test code is as follows, especially noting that the first parameter is callable That is, functions.


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

Write at the back

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

The above is Python programming itertools module processing iterative set related functions in detail, more about Python programming itertools module processing iterative set functions, please pay attention to other related articles on this site!


Related articles: