Re: Iterating over several lists at once

2006-12-27 Thread Roy Smith
In article <[EMAIL PROTECTED]>, "Gal Diskin" <[EMAIL PROTECTED]> wrote: > Hi, > I am writing a code that needs to iterate over 3 lists at the same > time, i.e something like this: > > for x1 in l1: > for x2 in l2: > for x3 in l3: > print "do something with", x1, x2, x3 >

Re: Iterating over several lists at once

2006-12-27 Thread Erik Johnson
"Gal Diskin" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > > On Dec 13, 3:47 pm, "Gal Diskin" <[EMAIL PROTECTED]> wrote: > > Hi, > > I am writing a code that needs to iterate over 3 lists at the same > > time, i.e something like this: > > > > for x1 in l1: > > for x2 in l2: >

Re: Iterating over several lists at once

2006-12-27 Thread Gal Diskin
On Dec 13, 3:47 pm, "Gal Diskin" <[EMAIL PROTECTED]> wrote: > Hi, > I am writing a code that needs to iterate over 3 lists at the same > time, i.e something like this: > > for x1 in l1: > for x2 in l2: > for x3 in l3: > print "do something with", x1, x2, x3 > > What I need

Re: Iterating over several lists at once

2006-12-14 Thread John Henry
Is this suppose to be a brain teaser or something? Michael Spencer wrote: > John Henry wrote: > > Carl Banks wrote: > > > >> The function can be extended to allow arbitrary arguments. Here's a > >> non-minmal recursive version. > >> > >> def cartesian_product(*args): > >> if len(args) > 1: >

Re: Iterating over several lists at once

2006-12-13 Thread Michael Spencer
John Henry wrote: > Carl Banks wrote: > >> The function can be extended to allow arbitrary arguments. Here's a >> non-minmal recursive version. >> >> def cartesian_product(*args): >> if len(args) > 1: >> for item in args[0]: >> for rest in cartesian_product(*args[1:]): >>

Re: Iterating over several lists at once

2006-12-13 Thread John Henry
Carl Banks wrote: > > The function can be extended to allow arbitrary arguments. Here's a > non-minmal recursive version. > > def cartesian_product(*args): > if len(args) > 1: > for item in args[0]: > for rest in cartesian_product(*args[1:]): > yield (item

Re: Iterating over several lists at once

2006-12-13 Thread Mike Erickson
* at ([EMAIL PROTECTED]) wrote: > Sorry for breaking into this thread, but I agree completely that any > unnecessary indentations should be avoided. For the same reason I advocate > that the following syntax should work: > > for x in some_list if some_condition: > ... code

Re: Iterating over several lists at once

2006-12-13 Thread at
Sorry for breaking into this thread, but I agree completely that any unnecessary indentations should be avoided. For the same reason I advocate that the following syntax should work: for x in some_list if some_condition: ... code ... in stead of for x in some_lis

Re: Iterating over several lists at once

2006-12-13 Thread Carl Banks
Gal Diskin wrote: > On Dec 13, 3:58 pm, Roberto Bonvallet <[EMAIL PROTECTED]> > wrote: > > Gal Diskin wrote: > > > Hi, > > > I am writing a code that needs to iterate over 3 lists at the same > > > time, i.e something like this: > > > > > for x1 in l1: > > >for x2 in l2: > > >for x3 in

Re: Iterating over several lists at once

2006-12-13 Thread Maksim Kasimov
Hi, if you "needs to iterate over 3 lists at the same" and according your example > for (x1,x2,x3) in (l1,l2,l3): > print "do something with", x1, x2, x3 i has guessed that you need this (may be i was wrong): a = (1,2,3, 1) b = (4,5,6) c = (7,8,9, 2, 3) for x, y, z in zip(a, b, c):

Re: Iterating over several lists at once

2006-12-13 Thread Gal Diskin
Thanks, that's an improvment (your first way). But I still wish I could find an even shorter (or more elegent) way of doing it. (Well, I guess if I expect every wish I have to come true I should at least wish for something more valuable.) Thanks again, Gal On Dec 13, 3:58 pm, "Fredrik Lundh" <[EM

Re: Iterating over several lists at once

2006-12-13 Thread Kay Schluehr
Gal Diskin schrieb: > Hi, > I am writing a code that needs to iterate over 3 lists at the same > time, i.e something like this: > > for x1 in l1: > for x2 in l2: > for x3 in l3: > print "do something with", x1, x2, x3 > > What I need to do is go over all n-tuples where the

Re: Iterating over several lists at once

2006-12-13 Thread Gal Diskin
Nothing seriously wrong, but it's not too elegent. Especially when the number of lists you want to iterate over gets bigger (especially because of the indentation in python). As you noticed (an phrased better than me), what I was wondering is if there is a way to iterate over the cartesian product,

Re: Iterating over several lists at once

2006-12-13 Thread Peter Otten
Gal Diskin wrote: > I am writing a code that needs to iterate over 3 lists at the same > time, i.e something like this: > for x1 in l1: > for x2 in l2: > for x3 in l3: > print "do something with", x1, x2, x3 > I was wondering if one could write this more easily in some ma

Re: Iterating over several lists at once

2006-12-13 Thread Paul Rubin
"Gal Diskin" <[EMAIL PROTECTED]> writes: > I am writing a code that needs to iterate over 3 lists at the same > time, i.e something like this: > > for x1 in l1: > for x2 in l2: > for x3 in l3: > print "do something with", x1, x2, x3 This does look a little kludgy (unteste

Re: Iterating over several lists at once

2006-12-13 Thread Roberto Bonvallet
Gal Diskin wrote: > Hi, > I am writing a code that needs to iterate over 3 lists at the same > time, i.e something like this: > > for x1 in l1: >for x2 in l2: >for x3 in l3: >print "do something with", x1, x2, x3 What's wrong with this? [...] > I'd be very happy to receiv

Re: Iterating over several lists at once

2006-12-13 Thread Fredrik Lundh
"Gal Diskin" wrote: > I am writing a code that needs to iterate over 3 lists at the same > time, i.e something like this: > > for x1 in l1: >for x2 in l2: >for x3 in l3: >print "do something with", x1, x2, x3 > > What I need to do is go over all n-tuples where the first arg

Iterating over several lists at once

2006-12-13 Thread Gal Diskin
Hi, I am writing a code that needs to iterate over 3 lists at the same time, i.e something like this: for x1 in l1: for x2 in l2: for x3 in l3: print "do something with", x1, x2, x3 What I need to do is go over all n-tuples where the first argument is from the first list,