Python module: itertools, pythonitertools
Itertools module: iterator
1. Infinite Loop: count, cycle, repeat
(1) count (5, 3) # increase the number of integers starting from 5 by 3, namely, 5, 8, 11, 14, 17... from itertools import * import timea = count (5, 3) for I in a: print (I) time. sleep (1) Output: 58111417202326
(2) cycle ('zxy') # repeat element x y z... from itertools import * import times = cycle ('xyz') for I in s: print (I) time. sleep (1) output result: xyzxyzxyz
Repeat () # repeat element Example 1: from itertools import * import times = repeat (3.14) # infinite repeat element for I in s: print (I) time. sleep (1) output result: 3.143.143.143.143.14 Example 2: from itertools import * import times = repeat () # repeat element 3, 5 times in total for I in s: print (I) time. sleep (1) Output: 33333
2. functional tools: starmap, takewhile, and dropwhile
(1) starmap () # similar to map, from itertools import * s = starmap (pow, [(), (), ()]) # pow () returns 1427 (2) takewhile () as the output result of the Index 1 ** 2 ** 3 for I in s: print (I) # When the function returns True, collect elements to the iterator. Once the function returns False, it stops. From itertools import * s1 = takewhile (lambda x: x <5, [, 7]) for I in s1: print (I) the output result is: 1234 (3) dropwhile () # Opposite to takewhile. S2 = dropwhile (lambda x: x <5, [567, 7]) for I in s2: print (I) output result: