This is the wklken of the Python Advanced-itertools module Summary of Learning itertools Module Learning notes
When looking at the source code of each function in Itertools, it is relatively easy at first, but it looks more laborious ...
1, Itertools.count (start=0,step=1)
This function is used to create an iterator that generates a sequential integer starting with n, and if n is omitted, the calculation starts at 0.
If the sys.maxint is exceeded, the counter overflows and continues the line-sys.maxint-1 start calculation
Defined:
def count (start=0, Step=1): #count (Ten) --One, one, ten ... # count (2.5, 0.5)--2.5, 3.0, 3.5 .... n = start while True: yield n + = Step
Use:
fromItertoolsImport* forIinchIzip (count (i), ['a','b','C']): Printiout: (1,'a')(2,'b')(3,'C')
2, Itertools.cycle (iterable)
Creates an iterator that iterates through the elements in the iterable, internally generating a copy of the elements in the iterable, which is used to return duplicates in the loop
Defined:
def cycle (iterable): # cycle (' ABCD ')--a B c d A b c D ... . Saved = [] for in iterable: yield element Saved.append (Element) while saved: for in saved: yield Element
Use:
fromItertoolsImport*I=0 forIteminchCycle (['a','b','C']): I+ = 1ifi = = 6: Break Print(I, Item) out: (1,'a')(2,'b')(3,'C')(4,'a')(5,'b')
3, Itertools.repeat (object[, Times])
Creates an iterator that repeatedly generates an object, and times (if provided) specifies a repeating count, and if the Times is not provided, the object is returned endlessly
Defined:
def Repeat (object, times=None): # repeat (3)---ten, ten if is None: while True: yield object Else: for in xrange (time): yield Object
Use:
from Import * for in repeat ('over-and-over', 3): Print iout:over-and-overover-and-overover-and -over
4, Itertools.chain (*iterables)
Multiple iterators are used as parameters, but only a single iterator is returned, which produces the contents of all the parameter iterators as if they were from a single sequence.
Defined:
def Chain (*iterables): # chain (' ABC ', ' def ')--A B C D E F for inch iterables: for inch it: yield Element
Use:
from Import * for in Chain ([1, 2, 3], ['a'b' 'C']): print iout:123ABC
5, itertools.compress (data, selectors)
Provides a selection list to filter the original data
Defined:
def Compress (data, selectors): # compress (' ABCDEF ', [1, 0, 1, 0, 1, 1])--A C E F return for inch if s)
6, Itertools.product (*iterables[, repeat])
Cartesian product
Create an iterator that generates ITEM1, ITEM2, and so on for the items in the Cartesian product tuple, repeat is a keyword parameter that specifies the number of times the sequence is generated repeatedly.
defProduct (*args, * *Kwds):#product (' ABCD ', ' xy ')--Ax, Ay, Bx, by, Cx, Cy, Dx, Dy #Product (range (2), repeat=3)-001 010 011 101Pools = map (tuple, args) * Kwds.get (Repeat, 1) Result= [[]] forPoolinchPools:result= [X+[y] forXinchResult forYinchPool] forProdinchResult:yieldTuple (PROD)
ImportItertoolsa= (1, 2, 3) b= ('a','b','C') C=Itertools.product (A, b) forEleminchC:Printelemout: (1,'A')(2,'B')(3,'C')(2,'A')(2,'B')(2,'C')(3,'A')(3,'B')(3,'C')
This module function has a lot of, have a lot of knock over to forget to save, too lazy to knock again, but also remember almost, so that's it
Python Learning notes-itertools module