473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

List Combinations

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
Mar 12 '08 #1
4 1663
On Mar 12, 10:18 am, Gerdus van Zyl <gerdusvan...@gmail.comwrote:
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
Mar 12 '08 #2
On Mar 12, 10:18*am, Gerdus van Zyl <gerdusvan...@gmail.comwrote:
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
Mar 12 '08 #3
Mel
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
Mar 12 '08 #4
On Mar 12, 3:38*pm, "Reedick, Andrew" <jr9...@ATT.COMwrote:
[...]
Start here

http://www.mail-archive.com/python-l...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[i] in iters[i]:
i += 1
break
else:
iters[i] = iter(sequences[i])
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 :)

--
Arnaud

Mar 12 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Broida (spamless) | last post by:
Hi! I have a list of item and I need to populate two columns with all of the combinations (not permutations), omitting the paired elements. Example: If my list in column A has W, X, Y, Z. ...
3
by: Ryan | last post by:
I've been trying to find an algorithm that will output all of the possible combinations of items in an array. I know how to find the number of combinations for each set using nCr=n!/(r!(n-r)!) ...
8
by: Shi Mu | last post by:
How to run a function to make become ,1,4],]? Thanks!
26
by: Swroteb | last post by:
Hi there, I've got a reasonably sized list of objects that I'd like to pull out all combinations of five elements from. Right now I have a way to do this that's quite slow, but manageable. I...
18
by: a | last post by:
can someone tell me how to use them thanks
8
by: Mir Nazim | last post by:
Hello, I need to write scripts in which I need to generate all posible unique combinations of an integer list. Lists are a minimum 12 elements in size with very large number of possible...
4
by: Rui Maciel | last post by:
I'm trying to go through the elements of a STL list container in such a way that each element can be acessed along with each and every subsequent element from the list. For example, let's say I...
15
by: desktop | last post by:
If I have a sorted std::list with 1.000.000 elements it takes 1.000.000 operations to find element with value = 1.000.000 (need to iterator through the whole list). In comparison, if I have a...
14
by: bjorklund.emil | last post by:
Hello pythonistas. I'm a newbie to pretty much both programming and Python. I have a task that involves writing a test script for every possible combination of preference settings for a software...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.