All combinations of a list

June Kim junaftnoon at nospamplzyahoo.com
Mon Oct 23 02:15:34 EDT 2000


Just for a brain-exercise, I changed the problem into "of combinations."

def comb(list):
    if len(list) ==0:
           return [[]]
    return [ [[list[0]],[]][i]+c for i in (0,1) \
           for c in comb(list[1:]) ]

Can anyone do this in different ways, still using recursion?
(I guess mapping would work here)

I'm waiting for your comments.

Best Regards,
June


"Emile van Sebille" <emile at fenx.com> wrote in message
news:8t0g73$lse00$1 at ID-11957.news.cis.dfn.de...
> def perms3(list):
>   return [[[list[i]] + p for i in range(len(list))\
>     for p in perms3(list[:i] + list[i+1:])],[[]]] [not list]
>
> This one works, ugly as it is.  Now, if the items are

Might look ugly, but very clever!
Thanks for your tip.

> reversed so that the empty list is element 0, is there a way
> to return either a 0 or 1 other that 'not not list'?
>
>
>
> --
>
> Emile van Sebille
> emile at fenx.com
> -------------------
>
>
>




More information about the Python-list mailing list