cyclic iterators ?

T

Tool69

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 ) :
'b'
etc.

I've tried something like this to have a cyclic iterator without
sucess:

def iterate_mylist(my_list):
k = len((my_list)
i=0
while i <= k :
yield my_list
i += 1
i = 0
yield my_list[0]

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

Paul Rubin

Tool69 said:
I've tried something like this to have a cyclic iterator without
sucess:

def iterate_mylist(my_list):
k = len((my_list)
i=0
while i <= k :
yield my_list
i += 1
i = 0
yield my_list[0]

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


As Bruno says, you can use itertools.cycle, but the problem above is
that you're not looping repeatedly through the list; you yield all the
elements, then yield the first element again, then stop. So for
['a','b','c'] you'd yield the sequence a,b,c,a.

I'd rewrite the above something like:

def iterate_mylist(my_list):
while True:
for m in my_list:
yield m

This just loops through the list over and over again.
 
B

Bruno Desthuilliers

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 ) :


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
 
T

tool69

Paul Rubin a écrit :
As Bruno says, you can use itertools.cycle, but the problem above is
that you're not looping repeatedly through the list; you yield all the
elements, then yield the first element again, then stop. So for
['a','b','c'] you'd yield the sequence a,b,c,a.

Yes, that was the problem.
Thanks for the explanation and for the cycle() function from itertool
that I missed.
 
M

MRAB

Tool69 said:
I've tried something like this to have a cyclic iterator without
sucess:
def iterate_mylist(my_list):
k = len((my_list)
i=0
while i <= k :
yield my_list
i += 1
i = 0
yield my_list[0]

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

As Bruno says, you can use itertools.cycle, but the problem above is
that you're not looping repeatedly through the list; you yield all the
elements, then yield the first element again, then stop. So for
['a','b','c'] you'd yield the sequence a,b,c,a.

I'd rewrite the above something like:

def iterate_mylist(my_list):
while True:
for m in my_list:
yield m

This just loops through the list over and over again.

Another problem is that it should be i < k, not i <= k.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top