combinations of variable length nested lists

Joseph A. Knapka jknapka at earthlink.net
Wed Aug 8 10:45:21 EDT 2001


xauau wrote:
> 
> 
> > This problem is a two-liner in Prolog:

[ipsnay]

> It is in Python too:
> 
> def permute(a):
>     if len(a) == 0: return [[]]
>     return [[x] + y for x in a[0] for y in permute(a[1:])]

Wow. So let me make sure I understand: this says:

>     if len(a) == 0: return [[]]

If there are no lists in a, return a list containing the
empty list. Clear enough.

>     return [

A list, each of whose elements is

>	[x] + y

a list consisting of a single element [x] prepended
to a list y

>          for x in a[0]

where x is a member of the first list in a

>            for y in permute(a[1:])]

and y is some permute() of all the other lists in a.

That is very cool, if a little non-obvious to the
uninitiated.

I've done some casual surfing around on python.org
looking for documentation of the functional-programming
features, but it's not in an obvious (to me) place.
Can someone provide a pointer?

Thanks,

-- Joe Knapka
"You know how many remote castles there are along the gorges? You
 can't MOVE for remote castles!" -- Lu Tze re. Uberwald
// Linux MM Documentation in progress:
// http://home.earthlink.net/~jknapka/linux-mm/vmoutline.html
2nd Lbl A + 1 = 2nd Pause 2nd Prt GTO 2 R/S



More information about the Python-list mailing list