Iterate using tuple as index

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James Stroud

    #1

    Iterate using tuple as index

    Hello,

    Its not obvious to me how to do this. I would like to iterate using a tuple as
    an index. Say I have two equivalently sized arrays, what I do now seems
    inelegant:

    for index, list1_item in enumerate(first list):
    do_something(li st1_item, secondlist[index])

    I would like something more like this:

    for list1_item, list2_item in (some_kind_of_e xpression):
    do_something(li st1_item, list2_item)

    Practically, I'm not so sure B is better than A, but the second would be a
    little more aesthetic, to me, at least.

    Any thoughts on what "some_kind_of_e xpression" would be?

    James

    --
    James Stroud, Ph.D.
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095
  • F. Petitjean

    #2
    Re: Iterate using tuple as index

    Le Thu, 10 Mar 2005 13:12:31 -0800, James Stroud a écrit :[color=blue]
    > Hello,
    >
    > Its not obvious to me how to do this. I would like to iterate using a tuple as
    > an index. Say I have two equivalently sized arrays, what I do now seems
    > inelegant:
    >
    > for index, list1_item in enumerate(first list):
    > do_something(li st1_item, secondlist[index])
    >
    > I would like something more like this:
    >
    > for list1_item, list2_item in (some_kind_of_e xpression):
    > do_something(li st1_item, list2_item)
    >
    > Practically, I'm not so sure B is better than A, but the second would be a
    > little more aesthetic, to me, at least.
    >
    > Any thoughts on what "some_kind_of_e xpression" would be?[/color]
    It is called "zip"
    for list1_item, list2_item in zip(firstlist, secondlist):
    ....
    try: help(zip)[color=blue]
    >
    > James
    >[/color]

    Comment

    • Tim Jarman

      #3
      Re: Iterate using tuple as index

      James Stroud wrote:
      [color=blue]
      > Hello,
      >
      > Its not obvious to me how to do this. I would like to iterate using a
      > tuple as an index. Say I have two equivalently sized arrays, what I do now
      > seems inelegant:
      >
      > for index, list1_item in enumerate(first list):
      > do_something(li st1_item, secondlist[index])
      >
      > I would like something more like this:
      >
      > for list1_item, list2_item in (some_kind_of_e xpression):
      > do_something(li st1_item, list2_item)
      >
      > Practically, I'm not so sure B is better than A, but the second would be a
      > little more aesthetic, to me, at least.
      >
      > Any thoughts on what "some_kind_of_e xpression" would be?
      >
      > James
      >[/color]

      for item1, item2 in zip(list1, list2):
      do_something(it em1, item2)

      perhaps?

      --
      Website: www DOT jarmania FULLSTOP com

      Comment

      • Bengt Richter

        #4
        Re: Iterate using tuple as index

        On Thu, 10 Mar 2005 13:12:31 -0800, James Stroud <jstroud@mbi.uc la.edu> wrote:
        [color=blue]
        >Hello,
        >
        >Its not obvious to me how to do this. I would like to iterate using a tuple as
        >an index. Say I have two equivalently sized arrays, what I do now seems
        >inelegant:
        >
        >for index, list1_item in enumerate(first list):
        > do_something(li st1_item, secondlist[index])
        >
        >I would like something more like this:
        >
        >for list1_item, list2_item in (some_kind_of_e xpression):
        > do_something(li st1_item, list2_item)
        >
        >Practically, I'm not so sure B is better than A, but the second would be a
        >little more aesthetic, to me, at least.
        >
        >Any thoughts on what "some_kind_of_e xpression" would be?
        >[/color]
        zip?
        [color=blue][color=green][color=darkred]
        >>> firstlist = [1,2,3]
        >>> secondlist = 'a b c'.split()
        >>> firstlist, secondlist[/color][/color][/color]
        ([1, 2, 3], ['a', 'b', 'c'])[color=blue][color=green][color=darkred]
        >>> for list1_item, list2_item in zip(firstlist, secondlist):[/color][/color][/color]
        ... print list1_item, list2_item
        ...
        1 a
        2 b
        3 c

        Or if your lists are very long, you could use an iterator from itertools, e.g.,
        [color=blue][color=green][color=darkred]
        >>> import itertools
        >>> for list1_item, list2_item in itertools.izip( firstlist, secondlist):[/color][/color][/color]
        ... print list1_item, list2_item
        ...
        1 a
        2 b
        3 c


        Regards,
        Bengt Richter

        Comment

        • Roy Smith

          #5
          Re: Iterate using tuple as index

          James Stroud <jstroud@mbi.uc la.edu> wrote:[color=blue]
          >I would like something more like this:
          >
          >for list1_item, list2_item in (some_kind_of_e xpression):
          > do_something(li st1_item, list2_item)[/color]

          I believe you want:

          for list1_item, list2_item in zip (list1, list2):
          blah

          Comment

          Working...