What's the Pythonic way to do this?
Peter Hansen
peter at engcorp.com
Fri Sep 10 16:50:19 EDT 2004
Doug Rosser wrote:
> 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
More information about the Python-list
mailing list