Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > cyclic iterators ?

Reply
Thread Tools

cyclic iterators ?

 
 
Tool69
Guest
Posts: n/a
 
      03-03-2007
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:

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

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

 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      03-03-2007
"Tool69" <> writes:
> 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]
> 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.
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      03-03-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
 
Reply With Quote
 
tool69
Guest
Posts: n/a
 
      03-03-2007
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.

 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      03-03-2007
On Mar 3, 1:27 am, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> "Tool69" <kibleur.christo...@gmail.com> writes:
> > 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]
> > 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
plain iterators and reverse iterators on vector subramanian100in@yahoo.com, India C++ 10 08-08-2009 08:28 AM
Cyclic array? jimi_usenet@hotmail.com Java 2 03-03-2006 06:20 PM
Iterators and reverse iterators Marcin Kaliciñski C++ 1 05-08-2005 09:58 AM
How the compiler deals with cyclic static initialization Hongzheng Wang Java 1 12-02-2003 01:28 PM
Cyclic object graph question Laird Nelson Java 2 10-10-2003 07:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57