[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

List Combinations

AngelBlaZe

3/12/2008 2:19:00 PM

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
4 Answers

Shane Geiger

3/12/2008 2:33:00 PM

0

Michael Wieher wrote:
>
>
> 2008/3/12, Gerdus van Zyl <gerdusvanzyl@gmail.com
> <mailto:gerdusvanzyl@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.
>
>


def gen(lists):
out = '[' + ','.join(["v%s" % i for i in range(len(lists))]) + ']'
comp = ''.join([ " for v%d in lists[%d]" % (i, i) for i in
range(len(lists))])
return eval('[ ' + out + comp + ' ]')

a,b,c = [1,2,3],[4,5,6],[7,8,9]

print gen([a, b, c])





--
Shane Geiger
IT Director
National Council on Economic Education
sgeiger@ncee.net | 402-438-8958 | http://ww...

Leading the Campaign for Economic and Financial Literacy

George Sakkis

3/12/2008 2:45:00 PM

0

On Mar 12, 10:18 am, Gerdus van Zyl <gerdusvan...@gmail.com> wrote:
> 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

Search for "cartesian product" recipes.

George

Mark Dickinson

3/12/2008 3:26:00 PM

0

On Mar 12, 10:18 am, Gerdus van Zyl <gerdusvan...@gmail.com> wrote:
> 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:

You could wait for Python 2.6, or download the current alpha:

Python 2.6a1+ (trunk:61353M, Mar 12 2008, 11:21:13)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from itertools import product
>>> for c in product(['3'], ['9', '1'], ['5'], ['4'], ['2', '5', '8']): print c
...
('3', '9', '5', '4', '2')
('3', '9', '5', '4', '5')
('3', '9', '5', '4', '8')
('3', '1', '5', '4', '2')
('3', '1', '5', '4', '5')
('3', '1', '5', '4', '8')

If you can't wait, look at http://docs.python.org/dev/library/iter...
where equivalent code is given.

Mark

Mel

3/12/2008 3:32:00 PM

0

Gerdus van Zyl wrote:
> 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

What they said, or, if you want to see it done:


def combi (s):
if s:
for a in s[0]:
for b in combi (s[1:]):
yield [a] + b
else:
yield []

for y in combi ([['3'], ['9', '1'], ['5'], ['4'], ['2', '5', '8']]):
print y