An introduction to the Python standard library's loop of itertools

  • 2020-04-02 14:23:24
  • OfStack

In (link: #) and (link: #), we learned about the functionality of the iterator. A circulator is a container for an object that contains multiple objects. By calling the next() method of the cycler (s), in Python 3.x), the cycler returns an object in turn. Until all objects are iterated, the iterator will raise a StopIteration error.

In the for I in iterator structure, the object returned each time by the loop is assigned to I until the loop ends. Using the iter() built-in function, we can turn containers such as tables, dictionaries, and so on into circulators. Such as:


for i in iter([2, 4, 5, 6]):
    print(i)

The itertools package in the standard library provides a more flexible tool for generating cyclers. Most of the input to these tools is from existing circulators. On the other hand, the tools are completely free to implement themselves in Python, and the package simply provides a more standard and efficient way to do it. This is also in line with Python's "only and preferably only solution" philosophy.


# import the tools
from itertools import *

Infinite circulator

Count (5, 2)         Integer circulator starting at 5, increments by 2 at a time, i.e. 5, 7, 9, 11, 13, 15...
Cycle (' ABC ')       Repeat the sequence of elements as a, b, c, a, b, c...
Repeat (1.2)         Repeat 1.2 to form an infinite loop, i.e. 1.2, 1.2, 1.2...

Repeat can also have a number limit:

Repeat (10, 5)     Repeat 10 for a total of 5 times

Functional tool

Functional programming is a programming paradigm that treats functions as objects. In Python, functions are also objects, so you can easily do some functional processing, such as map(), filter(), reduce() functions.

Itertools contains similar tools. These functions take a function as an argument and return the result as a circulator.

Such as:


from itertools import *
rlt = imap(pow, [1, 2, 3], [1, 2, 3])
for num in rlt:
    print(num)

The imap function is shown above. This function is similar to the map() function, except that instead of returning a sequence, it returns a circulator. Contains the result of the elements 1, 4, 27, which is 1**1, 2**2, 3**3. The function pow(built-in power function) takes the first argument. Pow () ACTS on each element of the next two lists in turn and collects the result of the function to form the loop that returns.

In addition, the following functions can be used:


starmap(pow, [(1, 1), (2, 2), (3, 3)])

The pow will act on each tuple of the table in turn.

The ifilter function is similar to the filter() function, except that it returns a circulator.


ifilter(lambda x: x > 5, [2, 3, 5, 6, 7]

The lambda functions are applied to each element in turn, and if the function returns True, the original elements are collected. 6, 7

In addition,


ifilterfalse(lambda x: x > 5, [2, 3, 5, 6, 7])

Similar to the above, but collects elements that return False. 2, 3, 5

takewhile(lambda x: x < 5, [1, 3, 6, 7, 1])

When the function returns True, the elements are collected into the circulator. Once the function returns False, it stops. 1, 3

dropwhile(lambda x: x < 5, [1, 3, 6, 7, 1])

When the function returns False, the element is skipped. Once the function returns True, all remaining elements are collected into the loop. 6, 7, 1.

Combination of tools

We can get a new cycler by combining the old cycler.


chain([1, 2, 3], [4, 5, 7])      # Connect the two circulators into one. 1, 2, 3, 4, 5, 7
product('abc', [1, 2])   # Cartesian product of a collection of circulators. It's kind of like a nested loop  
for m, n in product('abc', [1, 2]):
    print m, n

Permutations (' ABC ', 2)     Select two elements from 'abcd', such as ab, BC... Sorts all the results and returns them as a new circulator.

Note that the above order of composition, namely ab and ba, returns.

Combinations (' ABC ', 2)     Select two elements from 'abcd', such as ab, BC... Sorts all the results and returns them as a new circulator.

Note that the above combination returns only one ab in any order, that is, ab and ba.

Combinations_with_replacement (' ABC ', 2) # is similar to the above, but allows the selected element to repeat twice. That's aa, bb, cc.

Groupby ()

Apply the key function to each element of the original circulator. According to the key function result, the elements that have the same function result are divided into a new circulator. Each new loop is labeled with the result returned by the function.

It's like the height of a group of people acting as a circulator. We can use a key function that returns "tall" if the height is greater than 180; If the height is below 160, return "short"; The middle one returns "middle". Eventually, all heights will be divided into three circulators, namely "tall", "short" and "middle".


def height_class(h):
     if h > 180:
        return "tall"
     elif h < 160:
        return "short"
     else:
        return "middle"friends = [191, 158, 159, 165, 170, 177, 181, 182, 190] friends = sorted(friends, key = height_class)
 
for m, n in groupby(friends, key = height_class):
    print(m)
    print(list(n))

Note that groupby functions like the uniq command in UNIX. Before grouping, we need to use sorted() to sort the elements of the original circulator according to the key function, so that the elements of the same group are placed together first.

Other tools

Compress ('ABCD', [1, 1, 1, 0])   Select the element in the first parameter 'ABCD' according to the true and false values of [1, 1, 1, 0]. A, B, C
Islice ()                                               # is similar to the slice() function, except that it returns a loop
Izip ()                                                   # is similar to the zip() function, except that it returns a circulator.

conclusion

The tools in itertools are all self-implemented. Itertools simply provides a more robust solution.


Related articles: