i'm lost in list manipulation

GrelEns grelens at NOSPAMyahoo.NOTNEEDEDfr
Thu Mar 4 11:08:34 EST 2004


"Paul Rubin" <http://phr.cx@NOSPAM.invalid> a écrit dans le message de news:
7xbrnd54uj.fsf at ruckus.brouhaha.com...
> "GrelEns" <grelens at NOSPAMyahoo.NOTNEEDEDfr> writes:
> > i'm not happy with this code, must be an other shorter alternative, have
you
> > any siggestions on how to build these sets  [[1], [2], [3], [1, 2], [1,
3],
> > [2, 3], [1, 2, 3]] from [1,2,3] ?
>
> def subsets(lst):
>     def subgen(lst):
>         if not lst:
>             yield lst
>             return
>         for s in subsets(lst[1:]):
>             yield s
>             yield [lst[0]] + s
>     return list(subgen([1,2,3]))[1:]  # discard empty subset
>
> print subsets([1,2,3])

sorry, i miss something here probably loosing indentation as my version
contain an infnite recursion,

can you repost it ? thanx

btw it looks like very impressive.





More information about the Python-list mailing list