combinations of variable length nested lists

xauau xauau at yahoo.com.au
Wed Aug 8 09:06:42 CEST 2001


Hans Nowak <hnowak at cuci.nl> wrote in message news:<mailman.997193173.24832.python-list at python.org>...

> >===== Original Message From Mark Robinson <m.1.robinson at herts.ac.uk> =====

> >I have hurt my brain (and those who are unfortunate to sit near me ;))

> >trying to formulate an algorithm for the following problem. I am sure

> >that someone here must be able to help me out, it seems such a trivial

> >problem.

> >

> >I have a nested list structure of the following format:

> >[[2, 3, 4, 5],

> >  [4, 5, 6, 7, 34],

> >  [6, 2, 7, 4]


Here's a quick, two-liner in Python using functional style:

def permute(a):
    if len(a) <= 1: return [[x] for x in a[0]]
    return [[x] + y for x in a[0] for y in permute(a[1:])]



More information about the Python-list mailing list