[Tutor] variable numbers of for loops

Jose Amoreira ljmamoreira at gmail.com
Wed Nov 24 03:07:57 CET 2010


On Tuesday, November 23, 2010 02:01:40 pm Mac Ryan wrote:

> The code you wrote generates programs like:
> 
> for l0 in alphabet:
>     for l1 in alphabet:
>         for l2 in alphabet:
>             word = "".join([eval("l"+str(i)) for i in range(n)])
>             listOfWords.append(word)
> 
> which are recursive in essence (although the recursion is hard-coded
> rather than dynamic). This is not bad (problems in the domain of
> combinatorics - like yours - get normally solved recursively) but I
> can't imagine what would the advantage of this code over dynamic
> recursion.

I meant recursion in the sense of a function invoking itself (I guess it is 
what you call dynamic recursion). I also can't imagine any advantages of this 
code, I just thought it'd be a fun thing to try.

> 
> As for a more straightforward way to solve your specific problem: I
> would suggest you take a look to the combinatoric generators in the
> itertools module (http://docs.python.org/library/itertools.html).
> 
Thanks for this link, I didn't know about itertools.

So long,
Jose


More information about the Tutor mailing list