What's the Pythonic way to do this?

Discussion in 'Python' started by Doug Rosser, Sep 10, 2004.

  1. Doug Rosser

    Doug Rosser Guest

    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]
     
    Doug Rosser, Sep 10, 2004
    #1
    1. Advertising

  2. Doug Rosser <da_rosser <at> yahoo.com> writes:
    >
    > 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:

    >>> import itertools
    >>> itertools.cycle(inputList)


    If you're really asking about how to write the class, I might write it like:

    >>> class Cycle(object):

    .... 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
     
    Steven Bethard, Sep 10, 2004
    #2
    1. Advertising

  3. Doug Rosser

    Peter Hansen Guest

    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
     
    Peter Hansen, Sep 10, 2004
    #3
  4. Doug Rosser

    Aahz Guest

    In article <>,
    Peter Hansen <> wrote:
    >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)


    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.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "A foolish consistency is the hobgoblin of little minds, adored by little
    statesmen and philosophers and divines." --Ralph Waldo Emerson
     
    Aahz, Sep 11, 2004
    #4
  5. (Doug Rosser) wrote in message news:<>...
    > 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.
     
    Phillip J. Eby, Sep 12, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles Krug
    Replies:
    11
    Views:
    560
    Bengt Richter
    Apr 27, 2005
  2. Thomas Lotze

    Controlling a generator the pythonic way

    Thomas Lotze, Jun 11, 2005, in forum: Python
    Replies:
    12
    Views:
    456
    Thomas Lotze
    Jun 14, 2005
  3. Sean Berry
    Replies:
    6
    Views:
    328
    George Sakkis
    Oct 10, 2005
  4. Somesh

    The pythonic way

    Somesh, Dec 1, 2005, in forum: Python
    Replies:
    0
    Views:
    330
    Somesh
    Dec 1, 2005
  5. Carl J. Van Arsdall
    Replies:
    4
    Views:
    500
    Bruno Desthuilliers
    Feb 7, 2006
Loading...

Share This Page