What's the Pythonic way to do this?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Doug Rosser

    #1

    What's the Pythonic way to do this?

    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]
  • Steven Bethard

    #2
    Re: What's the Pythonic way to do this?

    Doug Rosser <da_rosser <at> yahoo.com> writes:[color=blue]
    >
    > 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][/color]

    Well, if all you want to do is cycle through the elements of a list, I'd
    suggest:
    [color=blue][color=green][color=darkred]
    >>> import itertools
    >>> itertools.cycle (inputList)[/color][/color][/color]

    If you're really asking about how to write the class, I might write it like:
    [color=blue][color=green][color=darkred]
    >>> class Cycle(object):[/color][/color][/color]
    .... 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

    Comment

    • Peter Hansen

      #3
      Re: What's the Pythonic way to do this?

      Doug Rosser wrote:[color=blue]
      > 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][/color]

      I think this is spelled like this now:

      import itertools
      mycycle = itertools.cycle (inputList)

      -Peter

      Comment

      • Aahz

        #4
        Re: What's the Pythonic way to do this?

        In article <qcOdnYlYFO0Wjt _cRVn-tQ@powergate.ca >,
        Peter Hansen <peter@engcorp. com> wrote:[color=blue]
        >Doug Rosser wrote:[color=green]
        >> 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][/color]
        >
        >I think this is spelled like this now:
        >
        >import itertools
        >mycycle = itertools.cycle (inputList)[/color]

        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 (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

        "A foolish consistency is the hobgoblin of little minds, adored by little
        statesmen and philosophers and divines." --Ralph Waldo Emerson

        Comment

        • Phillip J. Eby

          #5
          Re: What's the Pythonic way to do this?

          da_rosser@yahoo .com (Doug Rosser) wrote in message news:<e08cd05f. 0409101235.50b1 f8f3@posting.go ogle.com>...[color=blue]
          > class Cycle(object):
          > [snip][/color]

          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.

          Comment

          Working...