The essence of Python language: Itertools library

Foreword

Did you know that Python's Itertools library is considered a gem of Python? Some users even consider it one of the coolest and most amazing Python libraries. We can use Itertools modules to enrich our applications and create a reliable working solution in a shorter time.

This article will help readers understand how to use Itertools modules in their projects.

The article is divided into three parts, each part will explain the specific functions of the Itertools library. Specifically:

  1. Infinite iterator
  2. Terminate iterator
  3. Composite iterator

Concept introduction

What is an iterator?

Iterators are objects composed of next methods. It has a status. The state is used to remember the execution during the iteration. Therefore, the iterator knows its current state, which makes it memory efficient. This is why iterators are used in memory efficient and fast applications.

We can open unlimited data streams (such as reading a file) and get the next item (such as the next line in the file). Then we can perform an operation on the project and proceed to the next project. This may mean that we can have an iterator that returns an infinite number of elements, because we only need to know the current item.

When there is no next item to return, the iterator will raise a StopIteration exception.

What is iterable?

Iterable is an object that can return an iterator. It has an iter method that returns an iterator. Iterable is also an object that we can loop and call iter(). It has a getitem method, which can take consecutive indexes from 0 (and raise an IndexError when the index is no longer valid).

What is Itertools?

Itertools is a Python module and part of the Python 3 standard library. It allows us to perform efficient memory and computational tasks on the iterator. It is inspired by the construction of APL, Haskell and SML.

In essence, this module contains many fast and memory efficient methods that can help us build applications in pure Python concisely and efficiently.

Infinite iterator

What if we want to construct an iterator that returns infinitely evenly spaced values? Or, what if we have to generate an element loop from the iterator? Or, maybe we want to repeat the elements of the iterator?

The itertools library provides a set of functions that we can use to perform all the functions we need.

The three functions listed in this section construct and return iterators that can be an infinite stream of items.

Count

As an example, we can generate an infinite sequence of equidistant values:

start =10
stop =1
my_counter = it.count(start, stop)for i in my_counter:
 # this loop will run for ever
 print(i)

result:

101112131415

Cycle

We can use the cycle method to generate an infinite loop of elements from the input.

The input of this method needs to be an iterable object, such as a list, string or dictionary, etc.

my_cycle = it.cycle('Python')for i in my_cycle:print(i)

result:

P
y
t
h
o
n
P
y
t
h
o
n
P

Repeat

To repeat an item (such as a string or a collection), you can use the repeat() function:

to_repeat ='FM'
how_many_times =4
my_repeater = it.repeat(to_repeat, how_many_times)for i in my_repeater:print(i)
# Prints
FM
FM
FM
FM

This will repeat the string "FM" 4 times. If we don't provide the second parameter, it will repeat this string indefinitely.

Terminate iterator

In this section, I will explain the powerful features of termination iterations. These functions can be used in many scenarios, such as:

  1. We may have many iterations, and we want to perform an operation on all iteration elements in a sequence one by one.
  2. Or when we have a lot of functions that we want to execute on each element of iterable
  3. Or sometimes we delete elements from the iterator and then perform operations on other elements.

Chain

This method allows us to create an iterator that returns all the elements in the input iteration in the sequence until there are no more elements left. Therefore, it can treat a continuous sequence as a single sequence.

chain = it.chain([1,2,3],['a','b','c'],['End'])for i in chain:print(i)

result

123
a
b
c
End

Drop While

We can pass an iterable and a condition, and this method will start calculating the condition for each element until the condition returns False for an element. Once the condition value of an element is False, the function will return the rest of the iterable elements.

For example, suppose we have a job list and we want to iterate through the elements and return the elements only if the conditions are not met. Once the value of the condition is False, we expect to return the remaining elements of the iterator.

jobs =['job1','job2','job3','job10','job4','job5']
dropwhile = it.dropwhile(lambda x :len(x)==4, jobs)for i in dropwhile:print(i)

result:

job10
job4
job5

This method returns the above three items, because the length of the element job10 is not equal to 4 characters, so job10 and other elements are returned.

Take While

This method is the opposite of the dropwhile() method. Essentially, it returns all elements of an iterable until the first condition returns False, then it does not return any other elements.

For example, suppose we have a job list and want to stop returning to jobs immediately when the conditions are not met.

jobs =['job1','job2','job3','job10','job4','job5']
takewhile = it.takewhile(lambda x :len(x)==4, jobs)for i in takewhile:print(i)

result:

job1
job2
job3

GroupBy

This function constructs an iterator after grouping successive elements of the iterable. This function returns an iterator of key and value pairs, where the key is the group key and the value is a collection of consecutive elements grouped by key.

Consider the following code snippet:

iterable ='FFFAARRHHHAADDMMAAALLIIKKK'
my_groupby = it.groupby(iterable)for key, group in my_groupby:print('Key:', key)print('Group:',list(group))

Note that the group attribute is iterable, so I will materialize it as a list.

So this will print:

Key: F
Group:[‘F’, ‘F’, ‘F’]
Key: A
Group:[‘A’, ‘A’]
Key: R
Group:[‘R’, ‘R’]
Key: H
Group:[‘H’, ‘H’, ‘H’]
Key: A
Group:[‘A’, ‘A’]
Key: D
Group:[‘D’, ‘D’]
Key: M
Group:[‘M’, ‘M’]
Key: A
Group:[‘A’, ‘A’, ‘A’]
Key: L
Group:[‘L’, ‘L’]
Key: I
Group:[‘I’, ‘I’]
Key: K
Group:[‘K’, ‘K’, ‘K’]

Tee

This method can split an iteration and generate a new iteration from the input. The output is also an iterator, which returns the iterable value of a given number of items. To understand it better, please see the following code snippet:

iterable ='FM'
tee = it.tee(iterable,5)for i in tee:print(list(i))

This method returns the entire iterable FM, 5 times:

[ ‘F’, ‘M’][‘F’, ‘M’][‘F’, ‘M’][‘F’, ‘M’][‘F’, ‘M’]

Combination iterator

Permutations

By using the permutation method, we can create an iterator to return the continuous arrangement of elements in the input iterable.

We can pass in a parameter to specify the length of the arrangement. It defaults to the length of the iterable.

This means that when length is missing, the method will generate all possible full-length permutations.

iterable ='FM1'
length =2
permutations = it.permutations(iterable, length)for i in permutations:print(i)

result

( ‘F’, ‘M’, ‘1’)(‘F’, ‘1’, ‘M’)(‘M’, ‘F’, ‘1’)(‘M’, ‘1’, ‘F’)(‘1’, ‘F’, ‘M’)(‘1’, ‘M’, ‘F’)

If the length is 2, it generates:

( ‘F’, ‘M’)(‘F’, ‘1’)(‘M’, ‘F’)(‘M’, ‘1’)(‘1’, ‘F’)(‘1’, ‘M’)(‘F’, ‘M’)(‘F’, ‘1’)(‘M’, ‘1’)

Combinations

Finally, I want to explain how to generate combinations of iterables.

Given an iterator, we can construct an iterator to return a subsequence of elements of a given length.

According to their position, the elements are considered unique, and only different elements are returned.

iterable ='FM1'
combinations = it.combinations(iterable,2)for i in combinations:print(i)

result

( ‘F’, ‘M’)(‘F’, ‘1’)(‘M’, ‘1’)

·END·

Recommended Posts

The essence of Python language: Itertools library
What are the advantages of python language
The specific method of python import library
Consolidate the foundation of Python(7)
What is the advantage of python over corporate language
Consolidate the foundation of Python(6)
Consolidate the foundation of Python (3)
The usage of wheel in python
Python handles the 4 wheels of Chinese
Python simulation of the landlord deal
What is the use of Python
The premise of Python string pooling
Secrets of the new features of Python 3.8
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
End the method of running python
Basic analysis of Python turtle library implementation
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
What are the required parameters of python
Logistic regression at the bottom of python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
Solve the conflict of multiple versions of python
What is the scope of python variables
Python implements the sum of fractional sequences
Two days of learning the basics of Python
What is the id function of python
Analysis of glob in python standard library
Where is the pip path of python3
The specific method of python instantiation object
Python novice learns to use the library
python3 realizes the function of mask drawing
What is the prospect of python development
What is the function body of python
Solve the conflict of multiple versions of python
Detailed explanation of the use of pip in Python | summary of third-party library installation
What is the function of adb in python
Detailed explanation of the principle of Python super() method
The difference between the syntax of java and python
7 features of Python3.9
Python realizes the development of student management system
Detailed explanation of python standard library OS module
Python implements the shuffling of the cards in Doudizhu
The meaning and usage of lists in python
Can the value of the python dictionary be modified?
Python implements the source code of the snake game
Detailed explanation of the usage of Python decimal module
Analysis of common methods of Python operation Jira library
How about learning python at the age of 27?
2.3 Python language foundation
2.1 The Python Interpreter (python interpreter)
The consequences of uninstalling python in ubuntu, very
Python writes the game implementation of fishing master
[898] python get the intersection of two lists | union | difference
Detailed explanation of the principle of Python function parameter classification
A brief summary of the difference between Python2 and Python3
Detailed explanation of the principle of Python timer thread pool
How to turn the language of Ubuntu into Chinese? ?
Solve the problem of python compiling and installing ssl