List Combinations

Michael Wieher michael.wieher at gmail.com
Wed Mar 12 10:24:11 EDT 2008


2008/3/12, Gerdus van Zyl <gerdusvanzyl at gmail.com>:
>
> I have a list that looks like this:
> [['3'], ['9', '1'], ['5'], ['4'], ['2', '5', '8']]
>
> how can I get all the combinations thereof that looks like as follows:
> 3,9,5,4,2
> 3,1,5,4,2
> 3,9,5,4,5
> 3,1,5,4,5
> etc.
>
> Thank You,
> Gerdus
>
> --


list =  [['3'], ['9', '1'], ['5'], ['4'], ['2', '5', '8']]
newList = []
for l in list:
   newList.extend(l)

#newList = [3,9,1,5,4,2,5,8]
list=[]
for l in newList:
    if l not in list:
         list.append(l)

#now list is [3,9,1,5,4,2,8]

list.sort()

#now your list is in sorted order

Then, you can just write a series of for loops to spin off your data however
you like.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080312/dd64dd40/attachment.html>


More information about the Python-list mailing list