Python's built-in module itertools

1.itertools

  Python built module provides useful functions for manipulating objects iteration. And itertools quite efficient and save memory.

2.itertools模块中的方法

count (initial value = 0, step size = 1)

iterator will return count value uniformly spaced from the incoming initial start parameter. specified count may also receive a step size parameter.

Import the itertools COUNT from 
for COUNT in I (10): 10 # infinite loop begins, the default step size is equal. 1 
    IF I> 20 is: 
        BREAK 
    the else: 
        Print (I)

Islice () 

Returns an iterator, next the iterator () method from the available iterative if specified start, all elements preceding skipped; otherwise, start default value is zero.

Step 1 by default. If the step is specified as another value, to determine how many values are skipped between successive calls.

It works like a list slice (), but returns an iterator.

from the itertools Import islice, COUNT 

# mode a: islice (iteration object, the number of iterations) 

for I in islice (COUNT (10),. 5): 
    Print (I) 

for I in islice ( 'abcdefghigk',. 5): 
    Print (I ) 

# Second way: islice (iteration object, beginning index, ending index, step) 
# default step size is not written. 1 

S = islice ( 'abcdefghigk', 1, 4,2) 
A List = (S) 
Print (A )

cycle() 

It returns the elements from iterable until it is exhausted. This sequence is then repeated indefinitely.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/mainstream/p/11544134.html