Home >Backend Development >Python Tutorial >How to use the itertools module of Python standard library
Official description: Functional tools for creating and using iterators. That is, functions for creating efficient iterators.
Return multiple sequences as a single sequence.
For example:
import itertools for each in itertools.chain('i', 'love', 'python'): print each
Output:
i l o v e p y t h o n
Returns "combinations" of the specified length
For example:
import itertools for each in itertools.combinations('abc', 2): print each
Output:
('a', 'b') ('a', 'c') ('b', 'c')
Returns a "combination" of the specified length, and the elements in the combination can be repeated
For example:
import itertools for each in itertools.combinations_with_replacement('abc', 2): print each
Output:
('a', 'a') ('a', 'b') ('a', 'c') ('b', 'b') ('b', 'c') ('c', 'c')
Returns all combinations of the specified length, which can be understood as the Cartesian product
For example:
import itertools for each in itertools.product('abc', repeat=2): print each
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'b')
('b', 'c')
('c', 'a')
('c', 'b')
( 'c', 'c')
Return a permutation of length r
For example:
import itertools for value in itertools.permutations('abc', 2): print value
Output:
('a', 'b') ('a', 'c') ('b', 'a') ('b', 'c') ('c', 'a') ('c', 'b')
Returns the corresponding element of data whose selector is True
For example:
import itertools for each in itertools.compress('abcd', [1, 0, 1, 0]): print each
Output:
a c
Returns a sequence starting with start and increasing step, infinitely increasing
For example:
import itertools for each in itertools.count(start=0, step=2): print each
Output:
1 2 3 . .
Iterate the iterator infinitely
For example:
import itertools for each in itertools.cycle('ab'): print each
Output:
a b a b . .
Until predicate is true, return iterable subsequent data, otherwise drop it
For example:
import itertools for each in itertools.dropwhile(lambda x: x<5, [2,1,6,8,2,1]): print each
Output:
6 8 2 1
Return a group (key, itera), key is the value of iterable, itera is all items equal to key
For example:
import itertools for key, vale in itertools.groupby('aabbbc'): print key, list(vale)
Output:
a ['a', 'a'] b ['b', 'b', 'b'] c ['c']
Returns an element iterator whose predicate result is True. If predicate is None, returns all items that are True in iterable
For example:
import itertools for value in itertools.ifilter(lambda x: x % 2, range(10)): print value
Output:
1 3 5 7 9
Returns elements whose predicate is False. If predicate is None, returns all items in iterable that are False.
For example:
import itertools for value in itertools.ifilterfalse(lambda x: x % 2, range(10)): print value
Output:
0 2 4 6 8
Equivalent to map() in iterator mode
For example:
import itertools for value in itertools.imap(lambda x, y: x+y, (1,2,3), (4,5,6)): print value
Output :
5 7 9
Equivalent to iterator mode slicing operation
For example:
import itertools for value in itertools.islice('abcdefg', 1, 4, 2): print value
Output:
b d
Continuously return the object object. If times is specified, return times times
For example:
import itertools for value in itertools.repeat('a', 2): print value
Output:
a a
Returns the value of function(iter), iter is the element of iterable
For example:
import itertools for value in itertools.starmap(lambda x, y: x * y, [(1, 2), (3, 4)]): print value
Output:
2 12
If predicate is true, return the iterable element, if it is false, it will not return, break.
For example:
import itertools for value in itertools.takewhile(lambda x: x < 5, [1, 3, 5, 6]): print value
Output:
1 3
The above is the detailed content of How to use the itertools module of Python standard library. For more information, please follow other related articles on the PHP Chinese website!