List Combinations

G

Gerdus van Zyl

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
 
S

Shane Geiger

Michael said:
2008/3/12, Gerdus van Zyl <[email protected]
<mailto:[email protected]>>:

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
(e-mail address removed) | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
 
G

George Sakkis

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
 
M

Mark Dickinson

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/itertools.html
where equivalent code is given.

Mark
 
M

Mel

Gerdus said:
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
 
R

Reedick, Andrew

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of Shane Geiger
Sent: Wednesday, March 12, 2008 10:33 AM
To: Michael Wieher
Cc: (e-mail address removed)
Subject: Re: List Combinations


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
(e-mail address removed) | 402-438-8958 | http://www.ncee.net


It helps to quote your source...
http://www.mail-archive.com/[email protected]/msg178457.html


Start here

http://www.mail-archive.com/[email protected]/msg178356.html
and go through the thread. There are several ways to solve the problem
and we evaluated the performance and 'pythonicity' of each.




*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA625
 
A

Arnaud Delobelle

Start here

http://www.mail-archive.com/[email protected]/msg178356.html
and go through the thread.  There are several ways to solve the problem
and we evaluated the performance and 'pythonicity' of each.  

I used a kind of extended cartesian product function a while ago while
writing a parser. If I simplify it down to a simple product function
it goes like this:

def product(*sequences):
i, n = 0, len(sequences)
vals = [None]*n
iters = map(iter, sequences)
while i >= 0:
if i == n:
yield tuple(vals)
i -= 1
else:
for vals in iters:
i += 1
break
else:
iters = iter(sequences)
i -= 1

It's neither recursive nor a hack, I haven't tried to measure it
against other approaches (obviously it wouldn't beat the eval(...)
hack). I haven't optimised it for readability (by others) either :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top