Brief introduction
Official Description: Functional tools for creating and using iterators. Is the function used to create an efficient iterator.
Itertools.chain (*iterable)
Returns multiple sequences as a single sequence.
For example:
Import itertoolsfor itertools.chain (' I ', ' love ', ' Python '): Print each
Output:
Ilovepython
Itertools.combinations (iterable, R)
Returns the "combination" of the specified length
For example:
Import itertoolsfor itertools.combinations (' abc ', 2): Print each
Output:
(' A ', ' B ') (' A ', ' C ') (' B ', ' C ')
Itertools.combinations_with_replacement (iterable, R)
Returns the "combination" of the specified length, in which the elements in the composition are repeatable
For example:
Import itertoolsfor itertools.combinations_with_replacement (' abc ', 2): Print each
Output:
(' A ', ' a ') (' A ', ' B ') (' A ', ' C ') (' B ', ' B ') (' B ', ' C ') (' C ', ' C ')
Itertools.product (*iterable[,repeat])
Returns all combinations of the specified length, which can be understood as a Cartesian product
For example:
Import itertoolsfor 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 ')
Itertools.premutations (Iteravle[,r])
Returns an arrangement of length R
For example:
Import itertoolsfor value in Itertools.permutations (' abc ', 2): print value
Output:
(' A ', ' B ') (' A ', ' C ') (' B ', ' a ') (' B ', ' C ') (' C ', ' a ') (' C ', ' B ')
Itertools.compress (Data,selector)
Returns the data corresponding element that selector is true
For example:
Import itertoolsfor itertools.compress (' ABCD ', [1, 0, 1, 0]): Print each
Output:
Ac
Itertools.count (Start=0,step=1)
Returns the sequence of step increments starting with start, infinitely incrementing
For example:
Import itertoolsfor Itertools.count (start=0, step=2): Print each
Output:
123..
Itertools.cycle (iterable)
Infinite iteration of an iterator
For example:
Import Itertoolsfor each in itertools.cycle (' AB '): print each
Output:
Abab.
Itertools.dropwhile (predicate, iterable)
Until predicate is true, return to iterable subsequent data, or drop
For example:
Import itertoolsfor itertools.dropwhile (lambda x:x<5, [2,1,6,8,2,1]): Print each
Output:
6821
Itertools.groupby (Iterable[,key])
Returns a set (Key,itera), a value of iterable, Itera all items equal to key
For example:
Import Itertoolsfor key, Vale in Itertools.groupby (' AABBBC '): print key, List (Vale)
Output:
A [' A ', ' a ']b [' B ', ' B ', ' B ']c [' C ']
Itertools.ifilter (predicate, iterable)
Returns an element iterator that predicate the result to true, and returns all items that are true in iterable if predicate is none
For example:
Import itertoolsfor value in Itertools.ifilter (lambda x:x% 2, range): print value
Output:
13579
Itertools.ifilterfasle (predicate,iterable)
Returns an element that is predicate false, and returns all items that are false in iterable if predicate is none
For example:
Import itertoolsfor value in Itertools.ifilterfalse (lambda x:x% 2, range): print value
Output:
02468
Itertools.imap (Function,*iterables)
Map () equivalent to iterator mode
For example:
Import itertoolsfor value in Itertools.imap (lambda x, Y:x+y, (X-Ray), (4,5,6)): Print value
Output:
579
Itertools.islice (iterable, Start,stop[,step])
Slice operation equivalent to iterator mode
For example:
Import itertoolsfor value in Itertools.islice (' ABCDEFG ', 1, 4, 2): print value
Output:
Bd
Itertools.repeat (Object,[,times])
Returns object objects continuously, and returns times if time is specified
For example:
Import itertoolsfor value in Itertools.repeat (' a ', 2): print value
Output:
Aa
Itertools.starmap (function,iterable)
Returns the value of function (ITER), the iterable element of ITER
For example:
Import itertoolsfor value in Itertools.starmap (lambda x, y:x * y, [(1, 2), (3, 4)]): print value
Output:
212
Itertools.takewhile (predicate,iterable)
If predicate is true, the iterable element is returned, and if False it is no longer returned, break.
For example:
Import itertoolsfor value in Itertools.takewhile (lambda x:x < 5, [1, 3, 5, 6]): print value
Output:
13