vary number of loops

2008-04-16 Thread nullgraph
Hi everyone,

I'm new to Python and the notion of lambda, and I'm trying to write a
function that would have a varying number of nested for loops
depending on parameter n. This just smells like a job for lambda for
me, but I can't figure out how to do it. Any hint?

For example, for n=2, I want the function to look something like:

def foo(2)
   generate 2 sets of elements A, B
   # mix elements by:
   for a_elt in A
  for b_elt in B
 form all combinations of them


If n=3, I want to have 3 sets of elements and mix them up using 3 for
loops.

Any help is greatly appreciated,

nullgraph
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: vary number of loops

2008-04-16 Thread nullgraph
On Apr 16, 10:12 am, "Reedick, Andrew" <[EMAIL PROTECTED]> wrote:
> > -Original Message-
> > From: [EMAIL PROTECTED] [mailto:python-
> > [EMAIL PROTECTED] On Behalf Of Tim Chase
> > Sent: Wednesday, April 16, 2008 9:53 AM
> > To: [EMAIL PROTECTED]
> > Cc: [EMAIL PROTECTED]
> > Subject: Re: vary number of loops
>
> > > If n=3, I want to have 3 sets of elements and mix them up using 3
> for
> > > loops.
>
> > You might be ineterested in this thread:
>
> >http://mail.python.org/pipermail/python-list/2008-January/473650.html
>
> > where various solutions were proposed and their various merits
> > evaluated.
>
> I second that.  The thread compared building loops on the fly, building
> comprehensions nested to arbitrarily levels, recursion (slw!), a
> slick cookbook recipe using iterators, etc. and provided timings for
> each method.  Definitely worth bookmarking.
>


Yes, I second that second :) Very nice thread, I'm leaning toward the
"pythonic method" from there, but thanks for all the other solutions
suggested here. Guess I need to go play with lambda more...

nullgraph
-- 
http://mail.python.org/mailman/listinfo/python-list