I'm trying to get groups of permutations/combinations (r=2) that each member appear only once
I used python 'combinations' package to have the combinations.
For example: the members are: a,b,c,d. The combinations are: [a,b],[a,c],[a,d],[b,c],[b,d]...
My desired output is: [ {[a,b],[c,d]},{[a,c],[b,d]},{[a,d],[b,c]}...]
I would like to know what is the terminology for that case and if there is already implementation for that.
Thanks.
Here's a way to do it:
Explanation
You can use
itertools.combinations
twice, in order to get all the 2 tuple combinations from:And select only those whose set of elements intersect with that of the original list,
len(set(l) & set(chain(*i))) == len(l))
for every possible combination.You can find the permutations up to
r
and then filter the results:Output: