Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > iterating over two arrays in parallel?

Reply
Thread Tools

iterating over two arrays in parallel?

 
 
mh@pixar.com
Guest
Posts: n/a
 
      08-29-2008
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
 
Reply With Quote
 
 
 
 
skip@pobox.com
Guest
Posts: n/a
 
      08-29-2008
>>>>> "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

 
Reply With Quote
 
 
 
 
Luis Zarrabeitia
Guest
Posts: n/a
 
      08-29-2008

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

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      08-29-2008


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

 
Reply With Quote
 
Cousin Stanley
Guest
Posts: n/a
 
      08-29-2008

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
two arrays problem (although different from the other two arrays) Kev Jackson Ruby 2 03-29-2006 03:58 PM
Iterating over arrays in lockstep David Brady Ruby 2 08-30-2005 01:33 PM
iterating over arrays with map - problem Mothra Perl 1 05-27-2004 03:37 PM
iterating over arrays with map - problem Mothra Perl Misc 8 05-26-2004 03:37 PM
map/collect iterating over multiple arrays/arguments Zoran Lazarevic Ruby 5 10-08-2003 04:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57