I would like to generate a list of combinations. I will try to simplify my problem to make it understandable.
We have 3 variables :
- x : number of letters
- k : number of groups
- n : number of letters per group
I would like to generate using python a list of every possible combinations, without any duplicate knowing that : i don't care about the order of the groups and the order of the letters within a group.
As an example, with x = 4, k = 2, n = 2 :
# we start with 4 letters, we want to make 2 groups of 2 letters
letters = ['A','B','C','D']
# here would be a code that generate the list
# Here is the result that is very simple, only 3 combinations exist.
combos = [ ['AB', 'CD'], ['AC', 'BD'], ['AD', 'BC'] ]
Since I don't care about the order of or within the groups, and letters within a group, ['AB', 'CD']
and ['DC', 'BA']
is a duplicate.
This is a simplification of my real problem, which has those values : x = 12
, k = 4
, n = 3
. I tried to use some functions from itertools
, but with that many letters my computer freezes because it's too many combinations.
Another way of seeing the problem : you have 12 players, you want to make 4 teams of 3 players. What are all the possibilities ?
Could anyone help me to find an optimized solution to generate this list?
x = k * n
, or is itx >= k * n
? – Koopmancomb = [(a,b) for a in letters for b in letters if a != b]
. I don't quite know how to remove the duplicates – Quidnuncitertools
freezes your computer, I don't think you could implement something better using only Python – Holographk
determined byx
andn
? Can you provide an example where it’s not? – Painget_partitions_same_size
from my answer here could be helpful. – Berseem