Multiple tuples for one for statement

H

Harlin Seritt

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
 
S

Steven Bethard

Harlin said:
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
 
K

Kent Johnson

Harlin said:
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
 
D

Daniel Cer

Harlin said:
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
 
R

R. C. James Harlow

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-----
 
R

R. C. James Harlow

Hi All--



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-----
 
I

Ivan Van Laningham

Hi All--

R. C. James Harlow said:
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/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
 
P

Peter Hansen

Ivan said:
Define "works":
.... 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
 
I

Ivan Van Laningham

Hi All--

Peter said:
... 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/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
 
P

Peter Hansen

Ivan said:
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
 
I

Ivan Van Laningham

Peter said:
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/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,236
Messages
2,571,185
Members
47,820
Latest member
HortenseKo

Latest Threads

Top