473,327 Members | 1,979 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,327 software developers and data experts.

Iterator for circulating a list

L = somelist

idx = 0
while True:
item = L[idx]
# Do something with item
idx = (idx + 1) % len(L)

wouldn't it be cool if there was an itertool like this:

def circulate(L, begin = 0, step = 1):
idx = begin
while True:
yield L[idx]
idx = (idx + step) % len(L)

for x in circulate(range(10)):
print 'at', x,'!'

Or maybe someone knows an even better way of expressing the above code?

--
mvh Björn
Nov 13 '07 #1
1 1179
"BJörn Lindqvist" <bj*****@gmail.comwrites:
L = somelist

idx = 0
while True:
item = L[idx]
# Do something with item
idx = (idx + 1) % len(L)

wouldn't it be cool if there was an itertool like this:

def circulate(L, begin = 0, step = 1):
idx = begin
while True:
yield L[idx]
idx = (idx + step) % len(L)

for x in circulate(range(10)):
print 'at', x,'!'
How about itertools.cycle?
Nov 13 '07 #2

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

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
13
by: Adam Hartshorne | last post by:
Hi All, I was wondering if anybody can tell me what is wrong with the following code, in a .h file I have std::list<std::vector<Site> > positions ; std::list<std::vector<Site> >::iterator...
15
by: sandwich_eater | last post by:
I want to know how to set an std::list iterator variable to make it null or nil. If this is not possible what is the value of an uninitialised std::list iterator and is it ok to assign this value...
6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
7
by: andreas | last post by:
Hello, I have a problem with iterators in a fairly complex polygonal mesh data structure which is implemented using lists of geometric entities. However, the problem in itself is fairly simple:...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
15
by: jayesah | last post by:
Hi All, List and its iterator work as following way : list<intmylist; list<int>::iterator itr; itr = mylist.begin(); cout << (*itr); But I want something like this:
12
by: Howard | last post by:
Is there an easy way to get an iterator (*not* a reverse-iterator) to the last element in a list? The last() function returns the element itself, not an iterator. Thanks, -Howard
1
by: David Bilsby | last post by:
All Apologies for cross posing this but I am not sure if this is a VC 8 STL bug or simply an invalid use of the iterator. I have a PCI card access class which basically abstracts a third party...
3
by: JurgenvonOerthel | last post by:
I want to replace one element in a list<stringby a list<stringand I need to obtain an iterator to the first element in the inserted list. In code: void...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.