473,791 Members | 3,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

iterating over two arrays in parallel?

mh
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 '08 #1
4 2984
>>>>"mark" == mh <mh@pixar.comwr ites:

markI 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

markwhere 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 '08 #2

Quoting mh@pixar.com:
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 overthe
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

Aug 29 '08 #3


mh@pixar.com 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

Aug 29 '08 #4
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

Aug 29 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3532
by: Xiaozhu | last post by:
say, if you had parallel arrays containing the sales person id, the month, and the sales figures (for that person in that month), sorting on sales figure and preserve the order of figures within the data about the same person: Original Data: id month sales +-----+ +-----+ +-----+ | 432 | | 8 | | 89 | +-----+ +-----+ +-----+ | 123 | | 8 | | 116 |
34
4240
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
3
2792
by: cpuracr8 | last post by:
i've been looking through the topics here and i can't quite find one that helps me. i'm trying to sort a struct that contains three arrays using the sort() function. the three arrays are parallel and need to be grouped together when it is sorted. for instance, if i sort the int array by ascending order, i need the name and grade associated with that int array to also move with it. i hope what i said is not too confusing. what am i doing...
12
5312
by: lifeshortlivitup | last post by:
I am trying to construct two, one-dimensional parallel arrays that will give me a letter grade for whatever score I enter. The two arrays I need are minimum score and grade. I don't understand how to build the score array. The scores are as follows: 0 - 299 should result in a F 300 - 349 should result in a D 350 - 399 should result in a C 400 - 449 should result in a B ...
8
5265
by: earla12 | last post by:
Hi, I'm working on a program that takes the following input from the user: Student Name, Student Number, and Student GPA The information is then supposed to be stored in three separate arrays. Prior to sending the output to the screen, a bubble sort has to be performed so that it's sorted by Student Name.
3
2193
by: SneakyElf | last post by:
i am very green with c++ so i get stuck on very simple things anyway, i need to write a program that would read data from file (containing names of tv shows and their networks) one line at a time ( ; separates tv show and network) sort the strings according to the network and according to the show. and so: //to open file - ifstream myFile;
4
2823
RMWChaos
by: RMWChaos | last post by:
The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?" Alright, here's the story: with a great deal of help from gits, I've developed a DOM creation and deletion script, which can be used in multiple applications. You simply feed the script a JSON list of any size, and the script will create multiple DOM elements with as many attributes...
1
1992
by: joor | last post by:
Hi I am a beginner and currently trying to create a small program. I seem to be stuck on the multiplication of their elements. Eg. I have an Array with 4 different prices for 4 different products and have created 2 other Arrays to which I have assigned values through window.prompt(parseFloat) , what I am trying to do is multiplicate the values on the parallel arrays by their corresponding indexes and keep a running total so I can have an...
4
1673
by: mathieu | last post by:
Hi there, just trying to figure out how to iterate over two array without computing the len of the array: A = B = for a,b in A,B: # does not work ! print a,b
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.