Home >Backend Development >Python Tutorial >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.
Since itertools is a built-in library, no installation is required, just import 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 as X × 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)复制代码
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 the A
in 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 r If you need all permutations of a specified length, you can pass an optional length parameter .
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
and The difference between
itertools.combinations(iter,r) is: the former permutations
allows reuse, while the latter combinations
cannot be reused<pre class="brush:php;toolbar:false">>>> print(list(itertools.combinations('abc',3)))
[('a', 'b', 'c')]复制代码</pre>
combinations_with_replacement
, the only difference is that the formercombinations_with_replacement
The data in the collection type can be repeated <pre class="brush:php;toolbar:false">>>> 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')]复制代码</pre>
accumulate
>>> import itertools >>> x = itertools.accumulate(range(10)) >>> print(list(x)) [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]复制代码
compress
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
Let’s look at a simple example<pre class="brush:php;toolbar:false">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复制代码</pre>
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 复制代码
>>> 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:
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!