Discovering itertools
Itertools is one of the most interesting python libraries. It contains a series of functions inspired by functional languages that are used to work with iterators.
In this article I will mention some of the ones that have caught my attention the most and that are worth keeping in mind so as not to reinvent the wheel every time.
Count
Several times I have implemented an infinite count (well, it ends
explicitly at some point with a break) using a while True loop. itertools offers us a better alternative:
from itertools import count for i in count(start=2, step=2): if i > 10: break print(i)
2 4 6 8 10
As can be seen in the example, if it were not for the break, count would return infinite numbers. The above code is roughly equivalent to:
i = 2 while True: if i > 10: break print(i) i += 2
2 4 6 8 10
batched
A function to do something very common: get data from a sequence in batches of size n. Let's look at an example:
from itertools import batched sequence = [1, 2, 3, 4, 5, 6, 7] for batch in batched(sequence, 2): # lotes de tamaño 2 print(batch)
(1, 2) (3, 4) (5, 6) (7,)
It should be noted that the last batch may be of a size less than or equal to n, as in this case. Quite useful, don't you think?
pairwise
Another simple and useful function. Given a sequence (actually, given an iterator), it gives us its elements in pairs.
Let's see it better with an example:
from itertools import pairwise sequence = [1, 2, 3, 4, 5, 6, 7] for a, b in pairwise(sequence): print(a, b)
1 2 2 3 3 4 4 5 5 6 6 7
The number of pairs is one less than the size of the input sequence.
product
Finally, for this short entry, I want to talk about product, a
implementation of the Cartesian product.
Useful to replace a nested for that loops through two data streams:
from itertools import product A = [1, 2, 3] B = [5, 6, 7] for a, b in product(A, B): print(a, b)
1 5 1 6 1 7 2 5 2 6 2 7 3 5 3 6 3 7
Receives a parameter that allows us to make the Cartesian product of a
sequence with itself:
from itertools import product A = [1, 2, 3] for a1, a2, a3 in product(A, repeat=3): print(a1, a2, a3)
1 1 1 1 1 2 1 1 3 1 2 1 1 2 2 1 2 3 1 3 1 1 3 2 1 3 3 2 1 1 2 1 2 2 1 3 2 2 1 2 2 2 2 2 3 2 3 1 2 3 2 2 3 3 3 1 1 3 1 2 3 1 3 3 2 1 3 2 2 3 2 3 3 3 1 3 3 2 3 3 3
In a future post, I will mention other functions of this useful
module
python standard library. I hope it is useful to you.
The above is the detailed content of Discovering itertools. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
