Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Quick nested loop syntax?

Reply
Thread Tools

Quick nested loop syntax?

 
 
Johannes Bauer
Guest
Posts: n/a
 
      11-19-2008
Hi group,

if I remember correctly, wasn't there a way to quickly iterate through
nested loops? Something like

a = { "a", "b", "c" }
b = { 4, 9, 13}
for (x, y) in someoperator(a, b):
print(x, y)

which would print all tuples of
"a", 4
"a", 9
"a", 13
"b", 4
"b", 9
"b", 13
"c", 4
"c", 9
"c", 13

(not nececssarily in that order, of course).

Thanks,
Johannes

--
"Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
verlästerung von Gott, Bibel und mir und bewusster Blasphemie."
-- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
<48d8bf1d$0$7510$>
 
Reply With Quote
 
 
 
 
Mark Dickinson
Guest
Posts: n/a
 
      11-19-2008
On Nov 19, 5:48*pm, Johannes Bauer <dfnsonfsdu...@gmx.de> wrote:
> Hi group,
>
> if I remember correctly, wasn't there a way to quickly iterate through
> nested loops? Something like


Python 2.6 has itertools.product:

http://docs.python.org/library/itert...rtools.product

If you don't have Python 2.6 available, then the documentation
above also contains (almost) equivalent Python code that might
work for you.

Mark
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      11-19-2008
Johannes Bauer wrote:
> Hi group,
>
> if I remember correctly, wasn't there a way to quickly iterate through
> nested loops? Something like
>
> a = { "a", "b", "c" }
> b = { 4, 9, 13}
> for (x, y) in someoperator(a, b):
> print(x, y)


from itertools import product
a = { "a", "b", "c" }
b = { 4, 9, 13}
for (x, y) in product(a, b):
print(x, y)

 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      11-20-2008
On Nov 20, 3:48*am, Johannes Bauer <dfnsonfsdu...@gmx.de> wrote:
> a = { "a", "b", "c" }
> b = { 4, 9, 13}
> for (x, y) in someoperator(a, b):
> * * * * print(x, y)
>
> which would print all tuples of
> "a", 4
> "a", 9
> "a", 13
> "b", 4
> "b", 9
> "b", 13
> "c", 4
> "c", 9
> "c", 13


If you're using a version of Python before 2.6, you could also use a
list comprehension:

>>> show = lambda *args: sys.stdout.write('%s\n' % args)
>>> [show((x,y)) for x in a for y in b]

('a', 4)
('a', 9)
('a', 13)
('b', 4)
('b', 9)
('b', 13)
('c', 4)
('c', 9)
('c', 13)
 
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
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Adding quick-release to a non-quick-release tripod head ste7esmith@hotmail.com Digital Photography 4 11-20-2006 03:19 PM
Quick Question Quick Answer JKop C++ 11 05-24-2004 09:46 PM
Quick Restore for a Compaq not so quick! Croos Bustamunky Computer Support 2 05-15-2004 04:17 AM
PanasonicBQ390 "quick" charger - How quick? Ol' Bab Digital Photography 1 01-17-2004 06:54 AM



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