假设我们有一个列表:
list = [[a],[b],[c],[d],[e],[f],[g],[h]]
现在,我希望生成2乘3的所有可能组合,因此一个可能的组合是:
[[[a],[b],[c]], [[d],[e],[f]]]
另一个是:
[[[g],[h],[c]], [[d],[e],[f]]]
或
[[[a],[b],[f]], [[d],[e],[c]]]
在任何层面上,顺序都无关紧要。但是,元素不得重复,这意味着以下列表不正确,不应生成:
[[[a],[b],[f]], [[a],[e],[f]]]
同样地
[[a,b,c], [e,f,c]] and [[e,f,c], [a,b,c]]
将是相同的东西,并且应该只出现一次
我已经炸了不少神经细胞,但无法产生有效的解决方案。我正在使用Python来解决这个问题
^{} -函数就是您要寻找的!您的问题实际上可以通过创建6个元素的所有排列,然后简单地将所有这些排列拆分为两个列表来解决
此代码应该可以解决您的问题:
它不仅简单,而且速度快:
可以使用递归生成器函数:
输出:
itertools
是一个很受欢迎的库,用于从这些类型的问题中减轻一些心理负担一个利用itertools的解决方案(检查此解决方案是否适用于您的用例,以防您忘记提及订购要求):
如果您的列表很大,可以通过不将threecombs转换为列表来加快速度,但是如果您愿意这样做,我将把它留给您,因为您的问题表明您使用的是列表,而不是生成器
相关问题 更多 >
编程相关推荐