What's the Pythonic way to do this?

D

Doug Rosser

class Cycle(object):

def __init__(self, inputList):
object.__init__(self)
self.index = 0
self.limit = len(inputList)
self.list = inputList

def next(self):
"""
returns the next element of self.list, jumping
back to the head of the list if needed.
Yes, this is an infinite loop. (Use with caution)
Arguments:
none
Returns:
the next element of self.list
"""
if self.index+1 < self.limit:
self.index+=1
return self.list[self.index-1]
else:
self.index=0
return self.list[self.limit-1]
 
S

Steven Bethard

Doug Rosser said:
class Cycle(object):

def __init__(self, inputList):
object.__init__(self)
self.index = 0
self.limit = len(inputList)
self.list = inputList

def next(self):
"""
returns the next element of self.list, jumping
back to the head of the list if needed.
Yes, this is an infinite loop. (Use with caution)
Arguments:
none
Returns:
the next element of self.list
"""
if self.index+1 < self.limit:
self.index+=1
return self.list[self.index-1]
else:
self.index=0
return self.list[self.limit-1]

Well, if all you want to do is cycle through the elements of a list, I'd
suggest:

If you're really asking about how to write the class, I might write it like:
.... def __init__(self, lst):
.... self.index = -1
.... self.list = lst
.... def next(self):
.... self.index = (self.index + 1) % len(self.list)
.... return self.list[self.index]

Steve
 
P

Peter Hansen

Doug said:
class Cycle(object):

def __init__(self, inputList):
object.__init__(self)
self.index = 0
self.limit = len(inputList)
self.list = inputList

def next(self):
"""
returns the next element of self.list, jumping
back to the head of the list if needed.
Yes, this is an infinite loop. (Use with caution)
Arguments:
none
Returns:
the next element of self.list
"""
if self.index+1 < self.limit:
self.index+=1
return self.list[self.index-1]
else:
self.index=0
return self.list[self.limit-1]

I think this is spelled like this now:

import itertools
mycycle = itertools.cycle(inputList)

-Peter
 
A

Aahz

Doug said:
class Cycle(object):

def __init__(self, inputList):
object.__init__(self)
self.index = 0
self.limit = len(inputList)
self.list = inputList

def next(self):
"""
returns the next element of self.list, jumping
back to the head of the list if needed.
Yes, this is an infinite loop. (Use with caution)
Arguments:
none
Returns:
the next element of self.list
"""
if self.index+1 < self.limit:
self.index+=1
return self.list[self.index-1]
else:
self.index=0
return self.list[self.limit-1]

I think this is spelled like this now:

import itertools
mycycle = itertools.cycle(inputList)

That doesn't appear to work if self.list gets mutated. Then again, the
OP's version doesn't work, either. I'd use a linked list myself.
 
P

Phillip J. Eby

class Cycle(object):
[snip]

def cycle(inputList):
while True:
for item in inputList:
yield item

Or, as others have pointed out, there's an itertools.cycle that does
the same thing, at least in Python 2.3 and up.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top