The built-in modules itertools python provides useful functions for manipulating objects iteration
First, we look at a few of the infinite iterator provide itertools
>>> import itertools >>> natuals=itertools.count(1) >>> for n in natuals: ... print(n) ... 1 2 3 ...
Because the count () creates an infinite iterator, so the above code will print out the sequence of natural numbers, do not stop, can press Ctrl + c to exit
cycle()
Will pass a sequence continues indefinitely:
>>> cs=itertools.cycle('abc') >>> for c in cs: ... print(c) ... a b c a b c ...
The same can not stop
repeat () is responsible for an element continues indefinitely, but if the number of second parameter can define a repeating
>>> ns=itertools.repeat('A',3) >>> for n in ns: ... print(n) ... A A A
Only in the infinite sequence of iterations for infinite iteration will go on, if only to create an iteration object, it does not advance to generate an infinite number of elements in it, in fact it is impossible to create an unlimited number of elements in memory.