iterating over two arrays in parallel?

Discussion in 'Python' started by mh@pixar.com, Aug 29, 2008.

  1. Guest

    I want to interate over two arrays in parallel, something like this:

    a=[1,2,3]
    b=[4,5,6]

    for i,j in a,b:
    print i,j

    where i,j would be 1,4, 2,5, 3,6 etc.

    Is this possible?

    Many TIA!
    Mark

    --
    Mark Harrison
    Pixar Animation Studios
     
    , Aug 29, 2008
    #1
    1. Advertising

  2. Guest

    >>>>> "mark" == mh <> writes:

    mark> I want to interate over two arrays in parallel, something like this:
    mark> a=[1,2,3]
    mark> b=[4,5,6]

    mark> for i,j in a,b:
    mark> print i,j

    mark> where i,j would be 1,4, 2,5, 3,6 etc.

    a = [1,2,3]
    b = [4,5,6]
    for (i,j) in zip(a,b):
    print i, j

    To avoid recreating the entire list you can substitute itertools.izip for
    zip.

    Skip
     
    , Aug 29, 2008
    #2
    1. Advertising

  3. Quoting :

    > I want to interate over two arrays in parallel, something like this:
    >
    > a=[1,2,3]
    > b=[4,5,6]
    >
    > for i,j in a,b:
    > print i,j
    >
    > where i,j would be 1,4, 2,5, 3,6 etc.
    >
    > Is this possible?


    Yeap.

    ===
    for i,j in zip(a,b):
    print i,j
    ===

    Or better yet (memory wise at least)

    ===
    from itertools import izip

    for i,j in izip(a,b):
    print i,j
    ===

    ("zip" creates a list with the pairs (i,j), izip returns an iterator over the
    pairs "i,j")

    > --
    > Mark Harrison
    > Pixar Animation Studios


    Are you really from Pixar? Cool

    Cheers,

    --
    Luis Zarrabeitia
    Facultad de Matemática y Computación, UH
    http://profesores.matcom.uh.cu/~kyrie
     
    Luis Zarrabeitia, Aug 29, 2008
    #3
  4. Terry Reedy Guest

    wrote:
    > I want to interate over two arrays in parallel, something like this:
    >
    > a=[1,2,3]
    > b=[4,5,6]
    >
    > for i,j in a,b:
    > print i,j
    >
    > where i,j would be 1,4, 2,5, 3,6 etc.
    >
    > Is this possible?


    How to fish for yourself:
    search 'Python loop two arrays parallel' and second hit with Google is
    http://docs.python.org/tut/node7.html
    which has this entry
    "To loop over two or more sequences at the same time, the entries can be
    paired with the zip() function.

    >>> questions = ['name', 'quest', 'favorite color']
    >>> answers = ['lancelot', 'the holy grail', 'blue']
    >>> for q, a in zip(questions, answers):

    .... print 'What is your %s? It is %s.' % (q, a)
    ....
    What is your name? It is lancelot.
    What is your quest? It is the holy grail.
    What is your favorite color? It is blue.
    "

    Or go to the Tutorial directly, expand the chapter headings, and notice
    that 5. Data Structures has section 5.6 Looping Techniques.

    Indeed, I recommend that you read thru at least the first 9 chapters.

    tjr
     
    Terry Reedy, Aug 29, 2008
    #4

  5. > I want to interate over two arrays in parallel,
    > something like this:
    >
    > a=[1,2,3]
    > b=[4,5,6]
    >
    > for i,j in a,b:
    > print i,j
    >
    > where i,j would be 1,4, 2,5, 3,6 etc.
    >
    > Is this possible?
    >
    > Many TIA!
    > Mark


    >>>
    >>> list_1 = range( 1 , 4 )
    >>> list_2 = range( 4 , 7 )
    >>>
    >>> list12 = zip( list_1 , list_2 )
    >>>
    >>> for this in list12 :

    .... print ' ' , this
    ....
    (1, 4)
    (2, 5)
    (3, 6)
    >>>
    >>> for i , j in list12 :

    .... print ' ' , i , j
    ....
    1 4
    2 5
    3 6


    --
    Stanley C. Kitching
    Human Being
    Phoenix, Arizona
     
    Cousin Stanley, Aug 29, 2008
    #5
    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. Mothra
    Replies:
    1
    Views:
    534
    ! aaa
    May 27, 2004
  2. Dan
    Replies:
    3
    Views:
    289
    =?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?=
    Jul 25, 2004
  3. Zoran Lazarevic
    Replies:
    5
    Views:
    185
    Ara.T.Howard
    Oct 8, 2003
  4. Kev Jackson
    Replies:
    2
    Views:
    113
  5. Mothra
    Replies:
    8
    Views:
    171
    Richard Morse
    May 26, 2004
Loading...

Share This Page