473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

cyclic iterators ?

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.

Mar 3 '07 #1
4 1585
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
Mar 3 '07 #2
"Tool69" <ki****************@gmail.comwrites:
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.
Mar 3 '07 #3
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.

Mar 3 '07 #4
On Mar 3, 1:27 am, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
"Tool69" <kibleur.christo...@gmail.comwrites:
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.

Mar 3 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Thomas Mailund | last post by:
Hi group. I have a problem with some C extensions I am working with and hope that some of you can help. Basically, I am wrapping a a tree structure from C where I have python methods for...
7
by: Brian Sabolik | last post by:
I'm not sure if I've broken any Object Oriented rules or not, but ... I have projects in 2 different solutions that need to use each other's methods. Therefore I may have an "update" method in...
9
by: John Doe | last post by:
Hi all, Regarding those cyclic dependencies of classes (like cases in which class A refers to class B and class B to class A): can they ALL be resolved by forward defining classes, and by...
3
by: Dennis Lerche | last post by:
Hi I have a problem regarding cyclic dependency, yeahh I know bad design. But right at this moment I can't see how it should be redesigned to avoid this. The problem is that I just can't get it...
2
by: Matthias Kramm | last post by:
Hi All, I'm having a little bit of trouble using the "imp" module to dynamically import modules. It seems that somehow cyclic references of modules don't work. I'm unable to get the following...
3
by: fc2004 | last post by:
Hi, Is there any tools that could report where cyclic header dependency happens? this would be useful when working with a large project where tens or hundreds of headers files may form complex...
10
by: toton | last post by:
Hi I have a class called Session, which stores a vector of Page, like vector<PageAlso each Page need's to know the Session to which it is attached. Thus I have, (the classes has many other...
1
by: Joe Peterson | last post by:
I've been doing a lot of searching on the topic of one of Python's more disturbing issues (at least to me): the fact that if a __del__ finalizer is defined and a cyclic (circular) reference is...
1
by: pallav | last post by:
I have to header files, circuit.h and latch.h that reference each other and are causing a cyclic dependency. latch.h file #include "circuit.h" typedef boost::shared_ptr<struct LatchLatchPtr;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.