can I get the index number in for x in y loop?

Discussion in 'Python' started by JuHui, Apr 3, 2006.

  1. JuHui

    JuHui Guest

    >>> a='String'
    >>> for x in a:

    .... print x
    ....
    S
    t
    r
    i
    n
    g
    >>>


    can I get the index number of a in the upon loop within for x in a
    loop?
     
    JuHui, Apr 3, 2006
    #1
    1. Advertising

  2. JuHui

    Luigi Guest

    Try this:

    >>> a='String'
    >>> i=0
    >>> for x in a:

    .... print i, x
    .... i+=1
    ....
    0 S
    1 t
    2 r
    3 i
    4 n
    5 g
     
    Luigi, Apr 3, 2006
    #2
    1. Advertising

  3. JuHui

    Paul McGuire Guest

    "JuHui" <> wrote in message
    news:...
    > >>> a='String'
    > >>> for x in a:

    > ... print x
    > ...
    > S
    > t
    > r
    > i
    > n
    > g
    > >>>

    >
    > can I get the index number of a in the upon loop within for x in a
    > loop?
    >


    Use enumerate. See example below.

    -- Paul

    >>> a = "String"
    >>> for x in a:

    .... print x
    ....
    S
    t
    r
    i
    n
    g
    >>> for x in enumerate(a):

    .... print x
    ....
    (0, 'S')
    (1, 't')
    (2, 'r')
    (3, 'i')
    (4, 'n')
    (5, 'g')
    >>>
     
    Paul McGuire, Apr 3, 2006
    #3
  4. JuHui

    Rune Strand Guest

    JuHui wrote:
    > >>> a='String'
    > >>> for x in a:

    > ... print x
    > ...
    > S
    > t
    > r
    > i
    > n
    > g
    > >>>

    >
    > can I get the index number of a in the upon loop within for x in a
    > loop?


    for x, y in enumerate(a)
    print x, y
     
    Rune Strand, Apr 3, 2006
    #4
  5. JuHui

    JuHui Guest

    which one has best performance?

    a:for i in range(0,len(a))
    b:for x in a
    c:for x,y in enumerate(a)

    but, it seems I cann't get index number with b format..:(
     
    JuHui, Apr 3, 2006
    #5
  6. Em Seg, 2006-04-03 às 08:47 -0700, JuHui escreveu:
    > which one has best performance?


    Let's see...

    > a:for i in range(0,len(a))


    $ python2.4 -mtimeit -s 'a=[None]*100' 'for i in range(len(a)):
    j = a
    '
    100000 loops, best of 3: 17.7 usec per loop

    $ python2.4 -mtimeit -s 'a=[None]*100' 'for i in xrange(len(a)):
    j = a
    '
    100000 loops, best of 3: 16.8 usec per loop

    > b:for x in a


    $ python2.4 -mtimeit -s 'a=[None]*100' 'i = 0
    for j in a:
    i += 1
    '
    100000 loops, best of 3: 15.7 usec per loop

    > c:for x,y in enumerate(a)


    $ python2.4 -mtimeit -s 'a=[None]*100' 'for i, j in enumerate(a):
    pass
    '
    100000 loops, best of 3: 12.9 usec per loop


    Using enumerate is cleaner and faster.

    HTH,

    --
    Felipe.
     
    Felipe Almeida Lessa, Apr 3, 2006
    #6
  7. JuHui

    JuHui Guest

    thanks a lot!
    :)
     
    JuHui, Apr 3, 2006
    #7
  8. JuHui wrote:
    > which one has best performance?
    >
    > a:for i in range(0,len(a))
    > b:for x in a
    > c:for x,y in enumerate(a)


    Read up on the timeit module and figure it out for yourself.
    The answer will depend on the distribution of your data.

    > but, it seems I can't get index number with b format..:(


    Well, that's true, but it is a bit like saying:

    I cannot find the distance in meters between Paris and London with:
    for i in range(10):
    print i

    --Scott David Daniels
     
    Scott David Daniels, Apr 3, 2006
    #8
  9. JuHui

    John Hunter Guest

    >>>>> "Scott" == Scott David Daniels <> writes:

    Scott> I cannot find the distance in meters between Paris and
    Scott> London with: for i in range(10): print i

    Works for me

    def range(x):
    yield '332.8 km'

    for i in range(10):
    print i

    ....may not be considered best practice, though <wink>

    JDH
     
    John Hunter, Apr 3, 2006
    #9
  10. JuHui

    Fuzzyman Guest

    JuHui wrote:
    > >>> a='String'
    > >>> for x in a:

    > ... print x
    > ...
    > S
    > t
    > r
    > i
    > n
    > g
    > >>>

    >
    > can I get the index number of a in the upon loop within for x in a
    > loop?


    Although enumerate is the 'right' answer, I personally prefer :


    i = 0
    while i < len(some_sequence):
    val = some_sequence
    ...
    i += 1

    This is so that I can manually wind 'i' backwards and forwards manually
    from other parts of the code.

    Fuzzyman
    http://www.voidspace.org.uk/python/index.shtml
     
    Fuzzyman, Apr 3, 2006
    #10
    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. Jekyll
    Replies:
    2
    Views:
    434
    Victor Bazarov
    Apr 24, 2007
  2. KingdomHeart
    Replies:
    0
    Views:
    453
    KingdomHeart
    Jan 23, 2009
  3. Erik the Red
    Replies:
    4
    Views:
    186
    Chris Pine
    Jul 29, 2005
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    336
    Tomasz Chmielewski
    Mar 4, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    419
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page