473,763 Members | 5,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generating all possible combination of elements in a list

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 combination(12! )

I hacked a few lines of code and tried a few things from Python
CookBook (http://aspn.activestate.com/ASPN/Cookbook/), but they are
hell slow.

Does any body know of an algorithm/library/module for python that can
help me in generation of these combinations faster

"""ONLY REQUIREMENT IS SPEED"""

Example Problem:

Generate all possible permutations for
[1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]

[1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2] (notice an extra 2 )

eliminate some combinations based on some conditions and combine the
rest of combinations. And now generate all possible combinations for
resulting data set.
Hope you get the idea.

Thanks

PS: Tried matlab/scilab. They are slower than python.

Jul 23 '06 #1
8 5635
Mir Nazim wrote:
Example Problem:

Generate all possible permutations for
[1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]

[1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2] (notice an extra 2 )

eliminate some combinations based on some conditions and combine the
rest of combinations. And now generate all possible combinations for
resulting data set.
Hope you get the idea.
Unfortunately, I don't. Why do you have two lists for which to generate
permutations? Why is it important that the second list has an extra 2
(actually, not extra, but it replaces a 1)?
What are "some conditions" by which to eliminate permutations?
How to "combine" the remaining permutations?
What is the "resulting data set", and what is a "possible combination"
of it?

If the task is to produce all distinct permutations of 6 occurrences
of 1 and 6 occurrences of 2, I suggest the program below. It needs
produces much fewer than 12! results (namely, 924).

Regards,
Martin

numbers = [1,2]
remaining = [None, 6, 6]
result = [None]*12

def permutations(in dex=0):
if index == 12:
yield result
else:
for n in numbers:
if not remaining[n]:
continue
result[index] = n
remaining[n] -= 1
for k in permutations(in dex+1):
yield k
remaining[n] += 1

for p in permutations():
print p
Jul 23 '06 #2

Martin v. Löwis wrote:
Mir Nazim wrote:
Example Problem:

Generate all possible permutations for
[1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]

[1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2] (notice an extra 2 )

eliminate some combinations based on some conditions and combine the
rest of combinations. And now generate all possible combinations for
resulting data set.
Hope you get the idea.

Unfortunately, I don't. Why do you have two lists for which to generate
permutations? Why is it important that the second list has an extra 2
(actually, not extra, but it replaces a 1)?
What are "some conditions" by which to eliminate permutations?
How to "combine" the remaining permutations?
What is the "resulting data set", and what is a "possible combination"
of it?
condition are there cannot be more than 3 consecutive 2's or 1's
If the task is to produce all distinct permutations of 6 occurrences
of 1 and 6 occurrences of 2, I suggest the program below. It needs
produces much fewer than 12! results (namely, 924).
Yes that number I had already worked out and it is 792 for second list.
Now I have generated all distinct permutations and after eliminating
the permutations based on above condition I am left with 1060
permutations.

Now I ahave a lits with 1060 lists in it. Now comes the hard part.
How many possible distinct ways are there to arrange 1060 elements
taken 96 at a time

1060! / (1060 - 96)!
Hope you got the idea, why i need some faster ways to do it.
Now out of these i need to test only those lists whose sum of
elements(18 or 19) follows a particular pattern.

Can Anybody can alternate ways to do it on a Celeron 1.4 Ghz, 256 MB
RAM laptop.

Thnaks
Regards,
Martin

numbers = [1,2]
remaining = [None, 6, 6]
result = [None]*12

def permutations(in dex=0):
if index == 12:
yield result
else:
for n in numbers:
if not remaining[n]:
continue
result[index] = n
remaining[n] -= 1
for k in permutations(in dex+1):
yield k
remaining[n] += 1

for p in permutations():
print p
Jul 24 '06 #3
"Mir Nazim" <mi******@gmail .comwrites:
Now I ahave a lits with 1060 lists in it. Now comes the hard part.
How many possible distinct ways are there to arrange 1060 elements
taken 96 at a time

1060! / (1060 - 96)!
More than you want to think about:

import math

def logf(n):
"""return base-10 logarithm of (n factorial)"""
f = 0.0
for x in xrange(1,n+1):
f += math.log(x, 10)
return f

print logf(1060) - logf(1060 - 96)

Of course there are other ways you can calculate it, e.g.

http://en.wikipedia.org/wiki/Stirlings_approximation
Jul 24 '06 #4

Paul Rubin wrote:
1060! / (1060 - 96)!
More than you want to think about:

import math

def logf(n):
"""return base-10 logarithm of (n factorial)"""
f = 0.0
for x in xrange(1,n+1):
f += math.log(x, 10)
return f

print logf(1060) - logf(1060 - 96)

Of course there are other ways you can calculate it, e.g.
My problem is not to calculate this number, but generate this much
number of permutations in a fastest possible ways.

by the way, logf(1060) - logf(1060 - 96) = 288.502297251. Do you mean
there are only 289 possible permutation if 1060 elements taken 96 at a
time. Wow it is cool.

Please correct me if I got something wrong

Jul 24 '06 #5

Mir Nazim wrote:
Paul Rubin wrote:
1060! / (1060 - 96)!
More than you want to think about:

import math

def logf(n):
"""return base-10 logarithm of (n factorial)"""
f = 0.0
for x in xrange(1,n+1):
f += math.log(x, 10)
return f

print logf(1060) - logf(1060 - 96)

Of course there are other ways you can calculate it, e.g.

My problem is not to calculate this number, but generate this much
number of permutations in a fastest possible ways.

by the way, logf(1060) - logf(1060 - 96) = 288.502297251. Do you mean
there are only 289 possible permutation if 1060 elements taken 96 at a
time. Wow it is cool.

Please correct me if I got something wrong
Not 289, but 10**288.5022972 51.

That would make generating them all slightly intractable.

Jul 24 '06 #6
Mir Nazim wrote:
condition are there cannot be more than 3 consecutive 2's or 1's
>If the task is to produce all distinct permutations of 6 occurrences
of 1 and 6 occurrences of 2, I suggest the program below. It needs
produces much fewer than 12! results (namely, 924).

Yes that number I had already worked out and it is 792 for second list.
Now I have generated all distinct permutations and after eliminating
the permutations based on above condition I am left with 1060
permutations.
Again, I don't understand. You have 924 things, eliminate some of them,
and end up with 1060 things? Eliminating elements should decrease
the number, not increase it.
Now I ahave a lits with 1060 lists in it. Now comes the hard part.
How many possible distinct ways are there to arrange 1060 elements
taken 96 at a time

1060! / (1060 - 96)!
Well, this gives you

317904921427021 349485603608239 524627276760370 311702922721976 055955557097014 312266690535695 492655294084137 633231083274081 734289102812077 377976794152197 867852787116707 088721464684998 184672514662099 865363379483217 612335079690712 311047941504391 287024329222535 394623488000000 000000000000000 0000

lists. Assuming you have a 4GHz machine, and assuming you can
process one element per processor cycle (which you can't in
any programming language), you would still need

252017473226646 808001651769596 274596712297350 893980627474930 282816112614959 341916135952320 754578334351326 764040369160706 600622386171421 056868970503708 355413485474304 833144235702846 100228718497986 321476550199151 455745084737056 309493865347849 510895745515074 355200000000000 00

years to process them all. Even if you had 10000000000
computers (i.e. one per human being on the planet), you
still need ... you get the idea.
Now out of these i need to test only those lists whose sum of
elements(18 or 19) follows a particular pattern.
To succeed, you must take this condition into account.
What is the particular pattern?

Regards,
Martin
Jul 25 '06 #7
Again, I don't understand. You have 924 things, eliminate some of them,
and end up with 1060 things? Eliminating elements should decrease
the number, not increase it.
yes, u are right I had to types of lists:

one one them has 924 permutations and other has 792 making them 1722.
out of which only 1060 are permissible permutations
>
Now I ahave a lits with 1060 lists in it. Now comes the hard part.
How many possible distinct ways are there to arrange 1060 elements
taken 96 at a time

1060! / (1060 - 96)!

Well, this gives you

317904921427021 349485603608239 524627276760370 311702922721976 055955557097014 312266690535695 492655294084137 633231083274081 734289102812077 377976794152197 867852787116707 088721464684998 184672514662099 865363379483217 612335079690712 311047941504391 287024329222535 394623488000000 000000000000000 0000

lists. Assuming you have a 4GHz machine, and assuming you can
process one element per processor cycle (which you can't in
any programming language), you would still need

252017473226646 808001651769596 274596712297350 893980627474930 282816112614959 341916135952320 754578334351326 764040369160706 600622386171421 056868970503708 355413485474304 833144235702846 100228718497986 321476550199151 455745084737056 309493865347849 510895745515074 355200000000000 00

years to process them all. Even if you had 10000000000
computers (i.e. one per human being on the planet), you
still need ... you get the idea.
I under stand all these calculations.
Now out of these i need to test only those lists whose sum of
elements(18 or 19) follows a particular pattern.

To succeed, you must take this condition into account.
What is the particular pattern?
here is the pattern:

If
A = 18
B = 19

ONLY POSSIBLE Sequence of A, B type rows is as follows
A B A A B A B A A B A A B A A B A B A A B A A B A B A A B A
(REPEATING after this)

I need only thos permutations that follow this pattern. After that I
need to look of a few groupings of elements. like:

(2, 2) = 61 occurs times
(1, 1) = 54 occurs times
(2, 2, 2) = 29 occurs times
(1, 1, 1) = 13 occurs times

and so on. I am looking for the 96 row matrix that satisfies these
groupings.

Jul 25 '06 #8
Again, I don't understand. You have 924 things, eliminate some of them,
and end up with 1060 things? Eliminating elements should decrease
the number, not increase it.
yes, u are right I had to types of lists:

one one them has 924 permutations and other has 792 making them 1722.
out of which only 1060 are permissible permutations
>
Now I ahave a lits with 1060 lists in it. Now comes the hard part.
How many possible distinct ways are there to arrange 1060 elements
taken 96 at a time

1060! / (1060 - 96)!

Well, this gives you

317904921427021 349485603608239 524627276760370 311702922721976 055955557097014 312266690535695 492655294084137 633231083274081 734289102812077 377976794152197 867852787116707 088721464684998 184672514662099 865363379483217 612335079690712 311047941504391 287024329222535 394623488000000 000000000000000 0000

lists. Assuming you have a 4GHz machine, and assuming you can
process one element per processor cycle (which you can't in
any programming language), you would still need

252017473226646 808001651769596 274596712297350 893980627474930 282816112614959 341916135952320 754578334351326 764040369160706 600622386171421 056868970503708 355413485474304 833144235702846 100228718497986 321476550199151 455745084737056 309493865347849 510895745515074 355200000000000 00

years to process them all. Even if you had 10000000000
computers (i.e. one per human being on the planet), you
still need ... you get the idea.
I under stand all these calculations.
Now out of these i need to test only those lists whose sum of
elements(18 or 19) follows a particular pattern.

To succeed, you must take this condition into account.
What is the particular pattern?
here is the pattern:

If
A = 18
B = 19

ONLY POSSIBLE Sequence of A, B type rows is as follows
A B A A B A B A A B A A B A A B A B A A B A A B A B A A B A
(REPEATING after this)

I need only thos permutations that follow this pattern. After that I
need to look of a few groupings of elements. like:

(2, 2) = 61 occurs times
(1, 1) = 54 occurs times
(2, 2, 2) = 29 occurs times
(1, 1, 1) = 13 occurs times

and so on. I am looking for the 96 row matrix that satisfies these
groupings.

Jul 25 '06 #9

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

Similar topics

36
9477
by: rbt | last post by:
Say I have a list that has 3 letters in it: I want to print all the possible 4 digit combinations of those 3 letters: 4^3 = 64 aaaa
2
1459
by: GIMME | last post by:
Background ... I've created a web application that allows a user to create an HTML application from IE. The application itself creates an XML representation of a XHTML form. The XHTML representation can be saved as a string and recreated. (The application also has a crude workflow aspect - so XMHTML forms can be created and assigned a workflow. Forget I said anything about
7
3583
by: Venus | last post by:
Hello, I am trying to generate a dynamic form at runtime and would like to do it using "<asp: ..." form elements as follows Build up the string that is placed somewhere in the HTML code the same way like regular input fields can. strForm = "<form name=""myForm"" runat=""server"">" & vbCrLf strForm += "<asp:button name=""myName"" .... runat=""server"" />" & vbCrLf
2
310
by: Girish Sahani | last post by:
hello ppl, Consider a list like . Here 'a','b','c' are objects and 1,3,4,2 are their instance ids and they are unique e.g. a.1 and b.1 cannot exist together. From this list i want to generate multiple lists such that each list must have one and only one instance of every object. Thus, for the above list, my output should be: ,] Another example: Let l = . Then
1
6958
by: AAA | last post by:
hi, I'll explain fastly the program that i'm doing.. the computer asks me to enter the cardinal of a set X ( called "dimX" type integer)where X is a table of one dimension and then to fill it with numbers X; then the computer asks me how many subsets i have (nb_subset type (integer)) then,i have to enter for every sebset the card, and then to fill it, we'll have a two tables , one called cardY which contains nb_subset elements,and every...
9
6894
by: =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?= | last post by:
With regexps you can search for strings matching it. For example, given the regexp: "foobar\d\d\d". "foobar123" would match. I want to do the reverse, from a regexp generate all strings that could match it. The regexp: "{3}\d{3}" should generate the strings "AAA000", "AAA001", "AAA002" ... "AAB000", "AAB001" ... "ZZZ999". Is this possible to do? Obviously, for some regexps the set of matches is unbounded (a list of everything that...
6
3438
by: py_genetic | last post by:
Hi, I'm looking to generate x alphabetic strings in a list size x. This is exactly the same output that the unix command "split" generates as default file name output when splitting large files. Example: produce x original, but not random strings from english alphabet, all lowercase. The length of each string and possible combinations is
4
7688
by: RSH | last post by:
Okay my math skills aren't waht they used to be... With that being said what Im trying to do is create a matrix that given x number of columns, and y number of possible values i want to generate a two dimensional array of all possible combinations of values. A simple example: 2 - columns and 2 possible values would generate: 0 0
14
2008
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 I'm testing. I figured that this was something that a script could probably do pretty easily, given all the various possibilites. I started creating a dictionary of all the settings, where each key has a value that is a list of the possible...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9937
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8821
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.