cyclic iterators ?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sat Mar 3 02:49:13 CET 2007


Tool69 a écrit :
> Hi,
> 
> Let say I've got a simple list like my_list = [ 'a', ',b', 'c' ].
> We can have an iterator from it by k =  iter( my_list), then we can
> access each of her (his ?) element by k.next(), etc.
> 
> Now, I just wanted k to have the following cyclic behaviour (without
> rising the ) :
> 
> 
>>>k.next() 
> 'a'
>>>k.next()
> 'b'
>>>k.next()
> 'c'
>>>k.next()     -> not raising StopIteration error
> 'a'
>>>k.next()
> 'b'
> etc.
> 

> I've tried something like this to have a cyclic iterator without
> sucess:
> 
(snip code)


> I missed something, but I don't know what exactly.

from itertools import cycle

HTH



More information about the Python-list mailing list