Can anyone solve this?

rdudfield at my-deja.com rdudfield at my-deja.com
Mon Jun 26 08:22:25 EDT 2000


In article <39570E2A.BBF53AB1 at pik-potsdam.de>,
  Cezar Ionescu <ionescu at pik-potsdam.de> wrote:
> [Rene]
>
> [...snip]
> > I need the code to give every possible combination of the lists in
> > inputlist.
> [...snip again]
> >
> > Anyone able to solve this?
>
> Uhhh, I *think* so:
>
> def list_comb(inputlist):
>     outputlist = [[]]
>     for list in inputlist:
>         newoutputlist = []
>         for incomplete in outputlist:
>             for element in list:
>                 newoutputlist.append(incomplete + [element])
>         outputlist = newoutputlist
>     return outputlist
>
> if __name__ == "__main__":
>     a = [1, 2, 3]
>     b = ["c", 32]
>     c = [1, 2]
>     inputlist = [a, b, c]
>     print list_comb(inputlist)
>
> This prints the results you wanted. However, it probably won't do what
> you want for inputlist with one element, or with no elements, so
please
> take this as "a first approximation" only.
>
> Best,
> Cezar.
>

Thanks a lot for this, I tried for ages :)


Rene.



Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list