SoFunction
Updated on 2024-10-30

A short summary of the use of the itertools module in python.

Python's built-in itertools module contains a series of functions or classes for generating different types of iterators, all of which return an iterator that you can iterate through for loops to get to the value, or you can use next() to get to the value.

The itertools module provides the following types of iterator functions:

  • Infinite Iterator: generates an infinite sequence, e.g. the sequence of natural numbers 1, 2, 3, 4, ... ;
  • Finite Iterator: Receives one or more sequences as parameters to combine, group, filter, etc;
  • Combinatorial generators: permutations and combinations of sequences, finding the Cartesian product of sequences, etc.

itertools

Standard library for creating loopers under efficient loops

Infinite itertools, infinite iterators

(start=0, step=10)

By default, it returns an iterator of natural numbers starting from 0 and in order + 10, and it will keep running if you don't stop it.
You can specify the start position with start, and step is the step size.

import itertools

c = (start=0, step=1)
for i in c:
    print(i)

    
# 0
# 10
# 20
# 30
# 40
# 50
# ...

(iterable)

Pass in an iterable object and then iterate in an infinite loop.

import itertools

# ()
l = [1,2,3,4,5]
c = (l)

for i in c:
    print(i)


# 1
# 2
# 3
# 4
# 5
# 1
# 2
# 3
# 4
# 5
# ...

(p_object, times=None)

Repeatedly iterates over an object p_object an infinite number of times if times is not specified, or you can specify the number of iterations via the times argument.

import itertools

# ()
l = [1,2,3,4,5]
c = (l, 5)

for i in c:
    print(i)


# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]

Iterators terminating on the shortest input sequence

(iterable, func)

Returns the cumulative value of a sequence or other binary function.

import itertools

# ()
l = [1,2,3,4,5]
c = (l)

print(c)

for i in c:
    print(i)


# 1
# 3
# 6
# 10
# 15

accumulate() still returns an iterator, passing a list, and when traversing through the print in a for loop, you find that it does the accumulating operation. (Iterating over the first number is the sum of the previous number, and when iterating over the second number, it is the sum of the first two numbers, and so on.)

And it supports: list, tuple, str, set, dict when doing recursive operations.

is passed in as a dict object, then the key of the iterated dict will be accumulated:

import itertools

# ()
d = {'a': 1, 'b': 2, 'c': 3}
c = (d)

print(c)

for i in c:
    print(i)


# < object at 0x000001F7A0A90E48>
# a
# ab
# abc

(*iterables)

Arguments in the chain() method can be passed multiple sequences and as long as they are sequences, without limiting the data type of the sequence.

e.g. iterate through list, tuple, str.

import itertools

l = [1, 2, 3, 4, 5]
t = (1, 2, 3, 4, 5)
s = 'abcdefg'
c = (l, t, s)

print(c)

for i in c:
    print(i)

# < object at 0x0000026E64801448>
# 1
# 2
# 3
# 4
# 5
# 1
# 2
# 3
# 4
# 5
# a
# b
# c
# d
# e
# f
# g

itertools Cartesian Product

import itertools

d = [
    [{"a": 1}, {"b": 2}], [{"c": 3}, {"d": 4}, {"e": 5}], [{"f": 6}, {"g": 7}]
]

print(*d)

for i in (*d):
    print(i)

# [{'a': 1}, {'b': 2}] [{'c': 3}, {'d': 4}, {'e': 5}] [{'f': 6}, {'g': 7}]
# ({'a': 1}, {'c': 3}, {'f': 6})
# ({'a': 1}, {'c': 3}, {'g': 7})
# ({'a': 1}, {'d': 4}, {'f': 6})
# ({'a': 1}, {'d': 4}, {'g': 7})
# ({'a': 1}, {'e': 5}, {'f': 6})
# ({'a': 1}, {'e': 5}, {'g': 7})
# ({'b': 2}, {'c': 3}, {'f': 6})
# ({'b': 2}, {'c': 3}, {'g': 7})
# ({'b': 2}, {'d': 4}, {'f': 6})
# ({'b': 2}, {'d': 4}, {'g': 7})
# ({'b': 2}, {'e': 5}, {'f': 6})
# ({'b': 2}, {'e': 5}, {'g': 7})

to this article on the python itertools module in the simple use of the article is introduced to this, more related to the use of python itertools module content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!