Limiting the number of combinations in Python

Letters are like there are 33 ones, 33 twos and 66 zeros amounting to total letters 132. If I do it manually, there will be a lot of combinations. To make it easy for computer I want to limit combinations no to 1000. Plz help.
my_letters=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]

Sorry Ali123, I have no idea what you are talking about here. Your
variable letters doesn’t have any letters in it, it is a list with a
single large number:

11111111111111111111111111

There is a maximum of 1 combination that you can make by choosing
elements of a list of 1 value.

Can you please be more precise and accurate about what you are trying to
do?

Hello, @Ali123, and welcome to the Python Forum!

With limited information, all we can do is make a guess regarding what needs to be done.

Well, there are 26 occurrences of 1 here …

… and your variable is named Letters. Is this a binary number that relates to presence or absence of each letter in a combination of letters drawn from a 26-letter alphabet?

Let’s create 1000 combinations of letters, but to save space, we will display only 10 of those combinations.

import random
combos = []
for k in range(1000):
    letters = [random.randint(0, 1) for i in range(26)]
    combo = "".join([chr(i + ord("A")) for i in range(26) if letters[i] == 1 ])
    combos.append(combo)
for i in range(10):
    print(combos[i])

Output:

BEFHJNXZ
ABCFGHIORUVW
BDEFKNQRSTYZ
ABCHKLMORSTUVWXY
ADFHKMNOPQRSTUZ
EFKLMNOSUVWZ
CDEGHIJLMQVW
CDGHIKLMNOPRSTUVXZ
ABIJKOPQRSVXYZ
BCDEGIKMPTVWXY

With the limited information we have, it seems unlikely for that to be what you are really trying to do. But it was fun doing it anyway. :grinning:

Please supply additional details.

Letters are like there are 33 ones, 33 twos and 66 zeros amounting to total letters 132. If I do it manually, there will be a lot of combinations. To make it easy for computer I want to limit combinations no to 1000. Plz help.
my_letters=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]

1 Like

For what you are trying to do, what qualifies as a combination? Does a combination consist of 33 ones, 33 twos and 66 zeros, but in any order?

yes, order doesn’t matter. I simply want to find out 1000 different ways in which 33 ones 33 twos and 66 zeros will be arranged
Just like this [2 2 0 2 0 1 0 0 0 1 0 0 1 2 2 1 0 1 1 0 0 0 2 0 0 0 1 1 1 0 2 0 0 0 0 0 1 1 1 0 1 2 2 1 0 2 2 2 0 1 0 2 0 0 1 0 0 1 0 0 2 0 02 0 1 1 1 0 1 2 2 0 0 0 1 0 1 0 0 2 2 0 0 2 2 1 2 0 2 0 1 0 0 0 2 1 0 2 0 0 0 0 2 2 2 0 0 0 1 0 0 0 0 0 1 0 0 0 1 1 2 1 2 1 2 0 0 2 2 1 0]
this is one combination. I want 999 more of these combinations. Kindly help

1 Like

Technically, the different orders of the same numbers of each kind of object are known as permutations. Is this what is needed here?

Yes you are right. These are permutations and they are what I want to obtain

See Python Reference: random. shuffle ( x [, random ]).

Create your list, then within a loop that iterates 1000 times, you can create 1000 permutations.

Please give it a try and post your code.Then we can provide additional help.

Thank you for your help

1 Like

You are very welcome.

With 1000 shuffles of your my_letters sequence, there is a very small chance that there will be some duplication among the resulting permutations. The likelihood of such an occurrence is extremely small, but if you need an absolute guarantee that no such duplication will occur, you’ll need to take some action to prevent that. One possibility would be to sort the 1000 resulting permutations, then iterate through them to compare pairs in sequential order. If a match is found, discard and replace one of the matching permutations. Of course, even then, the replacement might match an existing permutation within your collection. There may be some more elegant solutions than this.

The odds of any of such duplication would be extremely small. You can check the references below to get a sense of how unlikely this is. Then decide whether that small likelihood is acceptable.

We hope you continue to enjoy the discussions on the Python Forum!

alternatively, to eliminate duplicates you could use the set() method.

example:

elements = [“ab”, “ba”, “bc”, “cb”, “ba”]
len(elements)
5

len(set(elements))
4

…then put your code that generates permutations of your my_letters sequence in a while loop that ensures each element is unique

while len(set(elements)) < 1000:
… …generate more permutations
… … elements = list(set(elements))

1 Like

This is excellent, since duplicates are averted as efficiently as regarding keys within a hash table, such as a Python dictionary.

EDITED on November 26, 2021 to add “regarding keys” to the above phrasing.