any such thing as list interleaving?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Plunket

    any such thing as list interleaving?


    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(aLis t)):
    # ...

    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(myLis t)):
    entry = aList(index)
    anotherEntry = anotherList(ind ex)
    # ...

    ???

    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!
  • Martin v. Löwis

    #2
    Re: any such thing as list interleaving?

    Tom Plunket wrote:
    [color=blue]
    > 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(aLis t)):
    > # ...[/color]

    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[i]
    except IndexError:
    return
    i += 1

    For older versions, yet another definition would be needed;
    I leave that as an exercise.
    [color=blue]
    > 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! :)[/color]

    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.iteritem s():
    #...

    HTH,
    Martin


    Comment

    • Gonçalo Rodrigues

      #3
      Re: any such thing as list interleaving?

      On Sat, 12 Jul 2003 14:00:49 -0700, Tom Plunket <[email protected] g>
      wrote:
      [color=blue]
      >
      >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(aLis t)):
      > # ...
      >
      >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:
      >[/color]

      Check the zip builtin:
      [color=blue][color=green][color=darkred]
      >>> help(zip)[/color][/color][/color]
      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.

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

      Fire the interpreter and type:
      [color=blue][color=green][color=darkred]
      >>> help(dict)[/color][/color][/color]

      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
      |
      [color=blue]
      >This seems a pretty sloppy way to go about it, imo. There must
      >be something more in the Python spirit! :)
      >
      >Thanks.
      >
      >-tom![/color]

      With my best regards,
      G. Rodrigues

      Comment

      Working...