Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

The IterTools module is a built-in python iterator module used for iterator customization. Now its main functions are sorted as follows:

1. Infinite iterators

1.1 the count function

itertools.count(start=0, step=1):

Generates an iterator that iterates from start and returns start + n * step for the NTH iteration

Eg: Generate odd numbers between 1 and 9

import itertools
​
​
for i in itertools.count(1, 2):
    if i > 9:
        break
    print(i)
Copy the code

result:

One, three, five, seven, nineCopy the code

1.2 cycle function

cycle(iterable)

Turn iterable into an iterator that iterates indefinitely, rather than iterating only once. The following is an example.

In the case of ordinary iterators

eg:

For I in a: if j == 5: break print(I) j += 1 print(f'j '==={j}')Copy the code

result:

1, 2, 3, j is equal to, ==, =3Copy the code

Since normal iterators can only iterate once, the value of j ends up being 3.

The situation after using the cycle function:

from itertools import cycle list1 = [1, 2, 3] b = cycle(list1) j = 0 for i in b: if j == 5: Print (I) j += 1 print(f'j '==={j}')Copy the code

result:

1, 2, 3, 1, 2, and the values of j are === =5Copy the code

At this point, the iterator iterates several times and encounters a break statement exit, so the value of j is 5

1.3 repeat function

repeat(object, ,times=None):

Object: An object used for repetition

Times: indicates the number of times. None indicates that the number of times can be repeated

Generates an iterator for an object that repeats times

from itertools import repeat
list1 = [1, 2, 3]
​
for num in repeat(list1, 5):
    print(num)
Copy the code

result:

[1, 2, 3] [1, 2, 3] [1, 2, 3]Copy the code

2. Permutation and composition iterators

2.1 the product function

product(*iterables, repeat):

Iterables: an iterator that requires a Cartesian product

Repeat: iterables Number of repeats. The default value is not repeated

Repeat all iterables before performing the Cartesian product. Default is not repeated

2.1.1 Non-repetitive situations

from itertools import product
​
for i in product('abc'):
    print(i)
Copy the code

result:

('a',)
('b',)
('c',)
Copy the code
from itertools import product
​
for i in product('abc', '12'):
    print(i)
Copy the code

result:

('a', '1')
('a', '2')
('b', '1')
('b', '2')
('c', '1')
('c', '2')
Copy the code

2.1.2 Repeated situations

from itertools import product
​
for i in product('abc', repeat=3):
    print(i)
Copy the code

result:

('a', 'a', 'a')
('a', 'a', 'b')
('a', 'a', 'c')
('a', 'b', 'a')
('a', 'b', 'b')
('a', 'b', 'c')
('a', 'c', 'a')
('a', 'c', 'b')
('a', 'c', 'c')
('b', 'a', 'a')
('b', 'a', 'b')
('b', 'a', 'c')
('b', 'b', 'a')
('b', 'b', 'b')
('b', 'b', 'c')
('b', 'c', 'a')
('b', 'c', 'b')
('b', 'c', 'c')
('c', 'a', 'a')
('c', 'a', 'b')
('c', 'a', 'c')
('c', 'b', 'a')
('c', 'b', 'b')
('c', 'b', 'c')
('c', 'c', 'a')
('c', 'c', 'b')
('c', 'c', 'c')
Copy the code

2.2 permutations function

The permutation operation in mathematics

from itertools import permutations
​
for i in permutations('abc', 3):
    print(i)
Copy the code

result:

('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')
Copy the code

2.3 combinations function

Combinatorial operations in mathematics

from itertools import combinations
​
for i in combinations('abc', 3):
    print(i)
Copy the code

result:

('a', 'b', 'c')
Copy the code

2.4 combinations_with_replacement function

Retrievable abstractions in mathematics (all previous permutations and combinations are non-retrievable abstractions)

from itertools import combinations_with_replacement
​
for i in combinations_with_replacement('abc', 3):
    print(i)
Copy the code

result:

('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')
Copy the code