473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Multiple tuples for one for statement

I have three tuples of the same size: tup1, tup2, tup3

I'd like to do something like this:

for a,b,c in tup1, tup2, tup3:
print a
print b
print c

Of course, you get an error when you try to run the pseudocode above.
What is the correct way to get this done?

Thanks,

Harlin

Jul 19 '05 #1
13 1580
for a,b,c in zip(tup1, tup2, tup3):
print a
print b
print c
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jul 19 '05 #2
Harlin Seritt wrote:
I have three tuples of the same size: tup1, tup2, tup3

I'd like to do something like this:

for a,b,c in tup1, tup2, tup3:
print a
print b
print c

Of course, you get an error when you try to run the pseudocode above.
What is the correct way to get this done?


for a, b, c in zip(tup1, tup2, tup3):
print a
print b
print c

If your tuples become iterators, look into itertools.izip.

STeVe
Jul 19 '05 #3
Harlin Seritt wrote:
I have three tuples of the same size: tup1, tup2, tup3

I'd like to do something like this:

for a,b,c in tup1, tup2, tup3:
print a
print b
print c


Presuming that you want a,b,c to be corresponding entries from the three tuples, then zip() is your
friend:
for a,b,c in zip(tup1, tup2, tup3):
...

Kent
Jul 19 '05 #4
Thank you Mr. Stroud.

Jul 19 '05 #5
Harlin Seritt wrote:
I have three tuples of the same size: tup1, tup2, tup3

I'd like to do something like this:

for a,b,c in tup1, tup2, tup3:
print a
print b
print c

Of course, you get an error when you try to run the pseudocode above.
What is the correct way to get this done?


For something like this, you can use izip from itertools to package
things up.

e.g. a working version of the code you posted using izip would be:

import itertools

tup1 = (1, 2, 3)
tup2 = (4, 5, 6)
tup3 = (7, 8, 9)

for a,b,c in itertools.izip(tup1, tup2, tup3):
print "a: %d b: %d c: %d" % (a, b, c)

This outputs:

a: 1 b: 4 c: 7
a: 2 b: 5 c: 8
a: 3 b: 6 c: 9

-Dan
Jul 19 '05 #6
On Monday 25 April 2005 04:20, James Stroud wrote:
for a,b,c in zip(tup1, tup2, tup3):
print a
print b
print c


or just:

for a,b,c in (tup1, tup2, tup3):
print a
print b
print c

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQBCbOjnY6W16wIJgxQRAnHcAJ9+oy7/SpUYRqx9HJAV8wtfNuVklQCfWMTD
sR44ZsI1Mda+mUIrw7Ae2jw=
=DyqF
-----END PGP SIGNATURE-----

Jul 19 '05 #7
Hi All--

"R. C. James Harlow" wrote:

or just:

for a,b,c in (tup1, tup2, tup3):
print a
print b
print c


And this works in Python version???

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 19 '05 #8
On Monday 25 April 2005 14:34, Ivan Van Laningham wrote:
Hi All--

"R. C. James Harlow" wrote:
or just:

for a,b,c in (tup1, tup2, tup3):
print a
print b
print c


And this works in Python version???


Ah, reading the replies to the original post, this works but doesn't give the
result that the original poster wanted.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQBCbPdIY6W16wIJgxQRAluIAKCijtwnaW5AcR+PgXARl/c2qrYzDQCffQaz
b+AAA8mJOs/wHlr7WuhyLNQ=
=443T
-----END PGP SIGNATURE-----

Jul 19 '05 #9
Hi All--

"R. C. James Harlow" wrote:

On Monday 25 April 2005 14:34, Ivan Van Laningham wrote:
Hi All--

"R. C. James Harlow" wrote:
or just:

for a,b,c in (tup1, tup2, tup3):
print a
print b
print c


And this works in Python version???


Ah, reading the replies to the original post, this works but doesn't give the
result that the original poster wanted.


Define "works":

a=(1,2,3,4,9,43,256,8,2021)
b=(1,0,3,4,7,999,256,8,2023)
c=(1,7,8,4,9,43,4444,8,2028)

for x,y,z in (a,b,c):
print x,y,z
12 [/c/CDListings][8] python fneeg.py
Traceback (most recent call last):
File "fneeg.py", line 8, in ?
for x,y,z in a,b,c:
ValueError: too many values to unpack

PyVersion:

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.


Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 19 '05 #10
Ivan Van Laningham wrote:
"R. C. James Harlow" wrote:
On Monday 25 April 2005 14:34, Ivan Van Laningham wrote:
"R. C. James Harlow" wrote:
or just:

for a,b,c in (tup1, tup2, tup3):
print a
print b
print c

And this works in Python version???


Ah, reading the replies to the original post, this works but doesn't give the
result that the original poster wanted.


Define "works":

a = (1,2,3)
b = ('a','b','c')
c = (None, 'foo', 3.14)
tup1 = (1,2,3)
tup2 = ('a','b','c')
tup3 = (None, 'foo', 3.14)
for a,b,c in (tup1,tup2,tup3): .... print a
.... print b
.... print c
....
1
2
3
a
b
c
None
foo
3.14


It's a valid interpretation of the OP's
ambiguously stated requirements, though probably
not the right one.

-Peter
Jul 19 '05 #11
Hi All--

Peter Hansen wrote:
Define "works":

>>> a = (1,2,3)
>>> b = ('a','b','c')
>>> c = (None, 'foo', 3.14)
>>> tup1 = (1,2,3)
>>> tup2 = ('a','b','c')
>>> tup3 = (None, 'foo', 3.14)
>>> for a,b,c in (tup1,tup2,tup3): ... print a
... print b
... print c
...
1
2
3
a
b
c
None
foo
3.14 >>>


It's a valid interpretation of the OP's
ambiguously stated requirements, though probably
not the right one.


I can see that now. I had three hours sleep last night and my brain
hurts, so I don't get it. I seek enlightenment.

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 19 '05 #12
Ivan Van Laningham wrote:
I can see that now. I had three hours sleep last night and my brain
hurts, so I don't get it. I seek enlightenment.


So do I: did you mean you don't even "get" what
my code is doing, or you don't get what the OP
really wanted, or something else?

(My sample works only because I created tuples that
had exactly three items each, of course. It's the
same as your previous code which didn't work, except
for the number of elements in each tuple. I wrote
it just to show that R.C.James's idea was a reasonable,
if probably mistaken, interpretation of the OP's request.)

-Peter
Jul 19 '05 #13


Peter Hansen wrote:

Ivan Van Laningham wrote:
I can see that now. I had three hours sleep last night and my brain
hurts, so I don't get it. I seek enlightenment.
So do I: did you mean you don't even "get" what
my code is doing...?


Yes. I barely remember my own name right now.

(My sample works only because I created tuples that
had exactly three items each, of course. It's the
same as your previous code which didn't work, except
for the number of elements in each tuple. I wrote
it just to show that R.C.James's idea was a reasonable,
if probably mistaken, interpretation of the OP's request.)


I worked out that the a,b,c must match the length of the tuples. The
fog past that point is too dense.

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 19 '05 #14

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

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
44
by: seberino | last post by:
Tuples are defined with regards to parentheses ()'s as everyone knows. This causes confusion for 1 item tuples since (5) can be interpreted as a tuple OR as the number 5 in a mathematical...
7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
66
by: Mike Meyer | last post by:
It seems that the distinction between tuples and lists has slowly been fading away. What we call "tuple unpacking" works fine with lists on either side of the assignment, and iterators on the...
2
by: Diego | last post by:
Hi everybody! I'm using DB2 PE v8.2.3 for linux. I've defined a database with the following schema: ANNOTATION(ID,AUTHOR,TEXT) ANNOTATION_BOOK(ANNOTATION_ID,OBJECT_ID)...
6
by: Allerdyce.John | last post by:
Hi, How can I put multiple condition in if statement? I try this, but I can't get that to work. if ( (str != " ") && (str != "") ): Any help is appreciated.
122
by: C.L. | last post by:
I was looking for a function or method that would return the index to the first matching element in a list. Coming from a C++ STL background, I thought it might be called "find". My first stop was...
15
by: Scott | last post by:
I'm going to start grouping all my questions in one post as this is my second today, and sorta makes me feel dumb to keep having to bother you all with trivial questions. I'll just seperate my...
4
by: =?utf-8?B?TWFjaWVqIEJsaXppxYRza2k=?= | last post by:
Hi Pythonistas! I've got a question about storing tuples in a dictionary. First, a small test case which creates a list of dictionaries: import time list_of_dicts = keys = prev_clk =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.