Use itertools.cycle
, that's its exact purpose:
from itertools import cycle
lst = ['a', 'b', 'c']
pool = cycle(lst)
for item in pool:
print item,
Output:
a b c a b c ...
(Loops forever, obviously)
In order to manually advance the iterator and pull values from it one by one, simply call next(pool)
:
>>> next(pool)
'a'
>>> next(pool)
'b'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…