列出所有组合组合 [英] List all combinations of combinations
问题描述
我正在尝试列出由6人组成的3人一组的所有可能组合。 (A,B,C,D,E,F)
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)
- 订单没关系的组没关系
- 货币对的顺序没关系
可能的组合:
{(B,D),(C,E),(G,H)}
{(B,C),(D,E),(G,H)}
{(B,E),(C,D),(G,H)}
我只能写到这么远:
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.
- 我知道应该列出15种组合
推荐答案
您可以将其实现为回溯问题。首先,您需要找到所有排列。之后,您应该对列表进行切片。例如:第一个排列是[A,B,C,D,E,F]。您将像这样[[A,B),(C,D),(E,F)]对其进行切片,第二个排列将为[A,B,C,D,F,E],切片为[(A, B),(C,D),(F,E)]。依此类推。
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.
这篇关于列出所有组合组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!