Get number of iteration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Florian Lindner

    Get number of iteration

    Hi!
    If I iterate through a list, is there a way I can get the number of the
    iteration: first, second, third, ...

    l = ["three", "four", "five", "six"]
    for x in l
    print x
    print x.iteration() # <- That's what I'm looking for!
    print "next"

    prints that:

    three
    1
    next
    four
    2
    next
    fixe
    3
    next
    six
    4
    next

    Thx,
    Florian
  • Diez B. Roggisch

    #2
    Re: Get number of iteration

    > If I iterate through a list, is there a way I can get the number of the[color=blue]
    > iteration: first, second, third, ...
    >
    > l = ["three", "four", "five", "six"]
    > for x in l
    > print x
    > print x.iteration() # <- That's what I'm looking for!
    > print "next"[/color]

    No, this won't work - x is the value of the list element, not an c++-like
    iterator-object (that has to be dereferenced before accessing the actual
    value).

    So usually x won't have an iteration-method. But of course you can alwas do
    this:

    for i in xrange(len(l)):
    x = l[i]
    print x
    print i
    print "next"

    Or - if len() can't be applied to your sequence for whatever reason, as it
    is e.g. a iterable object, you can of course keep track with your own
    counter:

    i = 0
    for x in some_iterable_t hingy:
    print x
    print i
    i += 1
    print "next"

    Regards,

    Diez

    Comment

    • Peter Otten

      #3
      Re: Get number of iteration

      Florian Lindner wrote:
      [color=blue]
      > If I iterate through a list, is there a way I can get the number of the
      > iteration: first, second, third, ...
      >
      > l = ["three", "four", "five", "six"]
      > for x in l
      > print x
      > print x.iteration() # <- That's what I'm looking for!
      > print "next"[/color]
      [color=blue][color=green][color=darkred]
      >>> sample = "tree for five".split()
      >>> for index, item in enumerate(sampl e):[/color][/color][/color]
      .... print index, item
      ....
      0 tree
      1 for
      2 five

      You are looking for enumerate(). Counting starts at 0, though.

      Peter

      Comment

      • Sidharth Kuruvila

        #4
        Re: Get number of iteration

        there is a function in python 2.3 called enumerate.

        [color=blue][color=green][color=darkred]
        >>> l = range(0,51,5)
        >>> e = enumerate(range (0,51,5))
        >>> e[/color][/color][/color]
        <enumerate object at 0x0119BBC0>[color=blue][color=green][color=darkred]
        >>> list(e)[/color][/color][/color]
        [(0, 0), (1, 5), (2, 10), (3, 15), (4, 20), (5, 25), (6, 30), (7, 35), (8,
        40), (9, 45), (10, 50)][color=blue][color=green][color=darkred]
        >>> e = enumerate(range (0,51,5))
        >>> for index, value in e:[/color][/color][/color]
        .... print "The index of value %i is %i" % (value, index)
        ....
        The index of value 0 is 0
        The index of value 5 is 1
        The index of value 10 is 2
        ..
        ..
        ..

        if you are using an older version of python which doesn't have enumerate
        you could do this[color=blue][color=green][color=darkred]
        >>> l = range(0,51,5)
        >>> zip(range(len(l )), l)[/color][/color][/color]
        [(0, 0), (1, 5), (2, 10), (3, 15), (4, 20), (5, 25), (6, 30), (7, 35), (8,
        40), (9, 45), (10, 50)]



        Comment

        • Sidharth Kuruvila

          #5
          Re: Get number of iteration

          there is a function in python 2.3 called enumerate.

          [color=blue][color=green][color=darkred]
          >>> l = range(0,51,5)
          >>> e = enumerate(range (0,51,5))
          >>> e[/color][/color][/color]
          <enumerate object at 0x0119BBC0>[color=blue][color=green][color=darkred]
          >>> list(e)[/color][/color][/color]
          [(0, 0), (1, 5), (2, 10), (3, 15), (4, 20), (5, 25), (6, 30), (7, 35), (8,
          40), (9, 45), (10, 50)][color=blue][color=green][color=darkred]
          >>> e = enumerate(range (0,51,5))
          >>> for index, value in e:[/color][/color][/color]
          .... print "The index of value %i is %i" % (value, index)
          ....
          The index of value 0 is 0
          The index of value 5 is 1
          The index of value 10 is 2
          ..
          ..
          ..

          if you are using an older version of python which doesn't have enumerate
          you could do this[color=blue][color=green][color=darkred]
          >>> l = range(0,51,5)
          >>> zip(range(len(l )), l)[/color][/color][/color]
          [(0, 0), (1, 5), (2, 10), (3, 15), (4, 20), (5, 25), (6, 30), (7, 35), (8,
          40), (9, 45), (10, 50)]





          Comment

          • Skip Montanaro

            #6
            Re: Get number of iteration


            Florian> If I iterate through a list, is there a way I can get the
            Florian> number of the iteration: first, second, third, ...

            Sure:
            [color=blue][color=green][color=darkred]
            >>> for i, what in enumerate(["three", "four", "five", "six"]):[/color][/color][/color]
            ... print i, what
            ...
            0 three
            1 four
            2 five
            3 six

            Skip

            Comment

            • Jeff Epler

              #7
              Re: Get number of iteration

              You're looking for the "enumerate" builtin function in 2.3.

              for i, x in enumerate(l):
              print x
              print i
              print "next"

              You can define a "poor man's" enumerate in 2.2:

              def enumerate(seq):
              return zip(range(len(s eq)), l)

              but the enumerate in 2.3 is an iterator, not a sequence.

              Jeff

              Comment

              • Paul Prescod

                #8
                Re: Get number of iteration

                Diez B. Roggisch wrote:
                [color=blue][color=green]
                >>If I iterate through a list, is there a way I can get the number of the
                >>iteration: first, second, third, ...
                >>
                >>l = ["three", "four", "five", "six"]
                >>for x in l
                >> print x
                >> print x.iteration() # <- That's what I'm looking for!
                >> print "next"[/color]
                > ...
                >
                > So usually x won't have an iteration-method. But of course you can alwas do
                > this:
                >
                > for i in xrange(len(l)):
                > x = l[i]
                > print x
                > print i
                > print "next"[/color]

                In recent versions of Python there is an easier way:
                [color=blue][color=green][color=darkred]
                >>> for index, value in enumerate(["three", "four", "five", "six"]):[/color][/color][/color]
                .... print index, value
                ....
                0 three
                1 four
                2 five
                3 six

                Paul Prescod



                Comment

                • Diez B. Roggisch

                  #9
                  Re: Get number of iteration

                  > In recent versions of Python there is an easier way:[color=blue]
                  >[color=green][color=darkred]
                  > >>> for index, value in enumerate(["three", "four", "five", "six"]):[/color][/color]
                  > ... print index, value
                  > ...
                  > 0 three
                  > 1 four
                  > 2 five
                  > 3 six[/color]

                  Boy, one never stops learning. I'll better stop answering here and read
                  only :)

                  Diez

                  Comment

                  • Jørgen Cederberg

                    #10
                    Re: Get number of iteration

                    Jeff Epler wrote:[color=blue]
                    > You're looking for the "enumerate" builtin function in 2.3.
                    >
                    > for i, x in enumerate(l):
                    > print x
                    > print i
                    > print "next"
                    >
                    > You can define a "poor man's" enumerate in 2.2:
                    >
                    > def enumerate(seq):
                    > return zip(range(len(s eq)), l)[/color]

                    Just nitpicking... :), but I guess you ment:

                    def enumerate(seq):
                    return zip(range(len(s eq)), seq)

                    /Jorgen

                    Comment

                    • Paul Rubin

                      #11
                      Re: Get number of iteration

                      Jørgen Cederberg <jorgencederber g@hotmail.com> writes:[color=blue]
                      > Just nitpicking... :), but I guess you ment:
                      >
                      > def enumerate(seq):
                      > return zip(range(len(s eq)), seq)[/color]

                      I think you want:

                      def enumerate(seq):
                      for i in xrange(len(seq) ):
                      yield (i, seq[i])

                      Comment

                      Working...