455,585 Members | 1,795 Online
Need help? Post your question and get tips & solutions from a community of 455,585 IT Pros & Developers. It's quick & easy.

Multiple tuples for one for statement

 P: n/a 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 Replies

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a Thank you Mr. Stroud. Jul 19 '05 #5

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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 cAnd this works in Python version???Ah, reading the replies to the original post, this works but doesn't give theresult 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

 P: n/a 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

 P: n/a 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

 P: n/a 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