combinations of variable length nested lists
xauau
xauau at yahoo.com.au
Wed Aug 8 06:16:40 EDT 2001
"Joseph A. Knapka" <jknapka at earthlink.net> wrote in message news:<3B700C33.1D161BAC at earthlink.net>...
> PEP proposal: add a backtracking Horn-clause resolution
> engine to Python!
>
> :-)
>
> This problem is a two-liner in Prolog:
>
> one_of_each([],[]).
> one_of_each([H|T],[H1|Ts]) :- member(H1,H), one_of_each(T,Ts).
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:])]
Actually, it needs all of three lines if you count the func def ;-)
Just for fun, does anyone know how to move the 'if' into the list
comprehension to make it a one-liner?
More information about the Python-list
mailing list