Home>Article>Backend Development> In-depth understanding of the itertools module in Python

In-depth understanding of the itertools module in Python

coldplay.xixi
coldplay.xixi forward
2020-11-12 17:32:17 1719browse

Python Video Tutorialcolumn introduces the itertools module.

In-depth understanding of the itertools module in Python

There is a powerful iteration tool package itertools in Python, which is one of the standard tool packages that comes with Python.

product

Since itertools is a built-in library, no installation is required, justimport itertools.

product is used to find the Cartesian product of multiple iterable objects(Cartesian Product), which is equivalent to a nested for loop. That is:

Cartesian The product refers to the Cartesian product (Cartesian product) of two sets X and Y in mathematics, also known as the direct product, expressed asX × Y.

product(A, B)is the same as ``((x,y) for x in A for y in B)`.

import itertools for item in itertools.product([1,2,3],[100,200]): print(item) # 输出如下 (1, 100) (1, 200) (2, 100) (2, 200) (3, 100) (3, 200)复制代码

permutations

In layman's terms, permutations return the full arrangement of all mathematics or characters of an iterable object.

Full permutation, that is, all permutations that produce a specified number of elements (depending on the order), which is theAin the high school permutation combination.

permutationsIt accepts a collection object and then produces a sequence of tuples.

For example,print(list(itertools.permutations('abc',3))), a total of A 3 3 = 6 A_3^3=6

items = ['a','b','c'] from itertools import permutations for i in permutations(items): print(i) #排列组合 print(list(itertools.permutations('abc',3))) # 输出如下 ('a', 'b', 'c') ('a', 'c', 'b') ('b', 'a', 'c') ('b', 'c', 'a') ('c', 'a', 'b') ('c', 'b', 'a') [('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]复制代码
If you need all permutations of a specified length, you can pass an optional length parameterr.

items = ['a','b','c'] from itertools import permutations for i in permutations(items,2): print(i) #排列组合 # 输出如下 ('a', 'b') ('a', 'c') ('b', 'a') ('b', 'c') ('c', 'a') ('c', 'b')复制代码

combinationsFind all combinations in which the specified number of elements in the list or generator are not repeated

itertools.permutations(iter,r)

and The difference between

itertools.combinations(iter,r)

is: the formerpermutationsallows reuse, while the lattercombinationscannot be reused

>>> print(list(itertools.combinations('abc',3))) [('a', 'b', 'c')]复制代码
combinations_with_replacement

combinations_with_replacement

is very similar to

combinations

, the only difference is that the formercombinations_with_replacementThe data in the collection type can be repeated

>>> print(list(itertools.combinations_with_replacement('abc',3))) [('a', 'a', 'a'), ('a', 'a', 'b'), ('a', 'a', 'c'), ('a', 'b', 'b'), ('a', 'b', 'c'), ('a', 'c', 'c'), ('b', 'b', 'b'), ('b', 'b', 'c'), ('b', 'c', 'c'), ('c', 'c', 'c')]复制代码
accumulate

accumulate

is used to accumulate elements in the list one by one

>>> import itertools >>> x = itertools.accumulate(range(10)) >>> print(list(x)) [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]复制代码

compress

compress()

Yes Filter tool, which accepts an iterable object and a Boolean selection sequence as input, and outputs all iterable objects that are True in the Boolean sequence.

import itertools its=["a","b","c","d","e","f","g","h"] selector=[True,False,1,0,3,False,-2,"y"] for item in itertools.compress(its,selector): print (item) a c e g h 复制代码

count

count(initial value=0, step=1)

is to create an iterator, evenly spaced starting from the passed in starting parameter numerical value.

Let’s look at a simple example

from itertools import count for i in count(10): #从10开始无限循环 if i > 20: break else: print(i) 10 11 12 13 14 15 16 17 18 19 20复制代码
chain

chain chain is mainly used to connect multiple sequences together for iteration.

import itertools chain = itertools.chain([1, 2, 3], [4, 5, 6]) for c in chain: print(c) 1 2 3 4 5 6 复制代码

Another very important function of chain is to flatten the list.

>>> list(itertools.chain([1, 2, 3], [4, 5], [6] ,[7,8])) [1, 2, 3, 4, 5, 6, 7, 8]复制代码

cycle

import itertools cycle = itertools.cycle([1, 2, 3]) for c in cycle: print(c)复制代码

The operation result output is 1 2 3 1 2 3... It keeps going round and round, never stopping.

Related free learning recommendations:

python video tutorial

The above is the detailed content of In-depth understanding of the itertools module in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete