List all combinations of combinations [duplicate]

微笑、不失礼 提交于 2019-12-10 20:54:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!