any such thing as list interleaving?

Discussion in 'Python' started by Tom Plunket, Jul 12, 2003.

  1. Tom Plunket

    Tom Plunket Guest

    I find myself often doing the following sort of thing (sorry for
    lack of whitespace, I don't want the line to break):

    for entry, index in map(lambda e,i:(e,i),aList,range(len(aList)):
    # ...

    This definitely seems like a roundabout way to loop through
    parallel lists together. Is this map routine truly the easiest/
    best/most straight-forward way to do a for loop through parallel
    lists, if I feel that the Python anti-idom of:

    for index in range(len(myList)):
    entry = aList(index)
    anotherEntry = anotherList(index)
    # ...

    ???

    This also brings up a similar problem for me when iterating over
    dictionaries:

    for key in myDict:
    value = myDict[key]
    # ...

    This seems a pretty sloppy way to go about it, imo. There must
    be something more in the Python spirit! :)

    Thanks.

    -tom!
     
    Tom Plunket, Jul 12, 2003
    #1
    1. Advertising

  2. Tom Plunket wrote:

    > I find myself often doing the following sort of thing (sorry for
    > lack of whitespace, I don't want the line to break):
    >
    > for entry, index in map(lambda e,i:(e,i),aList,range(len(aList)):
    > # ...


    In Python 2.3, you can write

    for index, entry in enumerate(L):
    # ...

    For 2.2, you can define enumerate yourself:

    def enumerate(L):
    i = 0
    while 1:
    try:
    yield i, L
    except IndexError:
    return
    i += 1

    For older versions, yet another definition would be needed;
    I leave that as an exercise.

    > This also brings up a similar problem for me when iterating over
    > dictionaries:
    >
    > for key in myDict:
    > value = myDict[key]
    > # ...
    >
    > This seems a pretty sloppy way to go about it, imo. There must
    > be something more in the Python spirit! :)


    Here, you could always write

    for key, value in myDict.items():
    #...

    Since 2.2, there is another method available which does not create
    a list of tuples:

    for key, value in myDict.iteritems():
    #...

    HTH,
    Martin
     
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Jul 12, 2003
    #2
    1. Advertising

  3. On Sat, 12 Jul 2003 14:00:49 -0700, Tom Plunket <>
    wrote:

    >
    >I find myself often doing the following sort of thing (sorry for
    >lack of whitespace, I don't want the line to break):
    >
    >for entry, index in map(lambda e,i:(e,i),aList,range(len(aList)):
    > # ...
    >
    >This definitely seems like a roundabout way to loop through
    >parallel lists together. Is this map routine truly the easiest/
    >best/most straight-forward way to do a for loop through parallel
    >lists, if I feel that the Python anti-idom of:
    >


    Check the zip builtin:

    >>> help(zip)

    Help on built-in function zip:

    zip(...)
    zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]

    Return a list of tuples, where each tuple contains the i-th
    element
    from each of the argument sequences. The returned list is
    truncated
    in length to the length of the shortest argument sequence.


    >for index in range(len(myList)):
    > entry = aList(index)
    > anotherEntry = anotherList(index)
    > # ...
    >
    >???
    >
    >This also brings up a similar problem for me when iterating over
    >dictionaries:
    >
    >for key in myDict:
    > value = myDict[key]
    > # ...
    >


    Fire the interpreter and type:

    >>> help(dict)


    As it's a long stretch of text, I'll just post the relevant part:

    | iteritems(...)
    | D.iteritems() -> an iterator over the (key, value) items of D
    |

    >This seems a pretty sloppy way to go about it, imo. There must
    >be something more in the Python spirit! :)
    >
    >Thanks.
    >
    >-tom!


    With my best regards,
    G. Rodrigues
     
    Gonçalo Rodrigues, Jul 12, 2003
    #3
    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. David Crichton

    ScrollBar Buttons? - Any such thing?

    David Crichton, Aug 31, 2003, in forum: HTML
    Replies:
    4
    Views:
    6,681
    Janne Raiskila
    Sep 2, 2003
  2. Replies:
    1
    Views:
    439
  3. Replies:
    5
    Views:
    323
    Tuomas
    Nov 22, 2006
  4. Giulio

    1024 bites interleaving frequency???

    Giulio, Nov 24, 2004, in forum: C Programming
    Replies:
    7
    Views:
    432
    Richard Tobin
    Nov 25, 2004
  5. Garrett Kajmowicz

    Interleaving iostream read and write

    Garrett Kajmowicz, Aug 14, 2005, in forum: C++
    Replies:
    0
    Views:
    472
    Garrett Kajmowicz
    Aug 14, 2005
Loading...

Share This Page