问题
I am trying to list out all the possible combinations of groups of 3 that one can make of 6 people. (A, B, C, D, E, F)
- The order doesn't of the group doesn't matter
- The order of the pair doesn't matter
Possible combinations:
{(B,D),(C,E),(G,H)}
{(B,C),(D,E),(G,H)}
{(B,E),(C,D),(G,H)}
I could only get as far to write:
from itertools import combinations
x = combinations('ABCDEF', 2)
z = [y for y in x]
I have no idea on how I should create combinations out of combinations, the docs is not to much help. I think I have to someone create an algorithm from scratch.
- I know that there should be 15 total combinations to list
回答1:
You can implement this as a backtracking problem. First, you need to find all the permutations. After that, you should slice the lists. For example: first permutation is [A,B,C,D,E,F]. You will slice it like this[(A,B),(C,D),(E,F)], second permutation will be [A,B,C,D,F,E] and slice is [(A,B),(C,D),(F,E)]. And so on.
来源:https://stackoverflow.com/questions/53658604/list-all-combinations-of-combinations