gpt4 book ai didi

Python:列表内容的所有可能组合

转载 作者:行者123 更新时间:2023-12-04 10:07:05 24 4
gpt4 key购买 nike

如何从另一个列表中获取包含所有可能内容组合的列表?

例子:

list = [A, B, C, D]

-->(魔法迭代)-->
combinations = [[A, B, C, D],
[[A], [B], [C], [D]],
[[A], [B, C, D]],
[[B], [A, C, D]],
[[C], [A, B, D]],
[[D], [A, B, C]],
[[A, B], [C, D]],
[[A, C], [B, D]],
[[A, D], [B, C]],
[[A], [B], [C, D]]
# and so on
]

最佳答案

这似乎满足您的所有要求:

import itertools                                                                                                                                                                                                                                                                                                              

def f(ls):
if not ls:
yield []

for i in range(1, len(ls) + 1):
for combo in itertools.combinations(ls, i):
for rest in f(set(ls) - set(combo)):
yield [list(combo), *rest]

输出:
[['A'], ['C'], ['B'], ['D']]                                                                                                                                                                                                                                                                                                  
[['A'], ['C'], ['D'], ['B']]
[['A'], ['C'], ['B', 'D']]
[['A'], ['B'], ['C'], ['D']]
[['A'], ['B'], ['D'], ['C']]
[['A'], ['B'], ['C', 'D']]
[['A'], ['D'], ['C'], ['B']]
[['A'], ['D'], ['B'], ['C']]
[['A'], ['D'], ['C', 'B']]
[['A'], ['C', 'B'], ['D']]
[['A'], ['C', 'D'], ['B']]
[['A'], ['B', 'D'], ['C']]
[['A'], ['C', 'B', 'D']]
[['B'], ['C'], ['A'], ['D']]
[['B'], ['C'], ['D'], ['A']]
[['B'], ['C'], ['A', 'D']]
[['B'], ['A'], ['C'], ['D']]
[['B'], ['A'], ['D'], ['C']]
[['B'], ['A'], ['C', 'D']]
[['B'], ['D'], ['C'], ['A']]
[['B'], ['D'], ['A'], ['C']]
[['B'], ['D'], ['C', 'A']]
[['B'], ['C', 'A'], ['D']]
[['B'], ['C', 'D'], ['A']]
[['B'], ['A', 'D'], ['C']]
[['B'], ['C', 'A', 'D']]
[['C'], ['B'], ['A'], ['D']]
[['C'], ['B'], ['D'], ['A']]
[['C'], ['B'], ['A', 'D']]
[['C'], ['A'], ['B'], ['D']]
[['C'], ['A'], ['D'], ['B']]
[['C'], ['A'], ['B', 'D']]
[['C'], ['D'], ['B'], ['A']]
[['C'], ['D'], ['A'], ['B']]
[['C'], ['D'], ['B', 'A']]
[['C'], ['B', 'A'], ['D']]
[['C'], ['B', 'D'], ['A']]
[['C'], ['A', 'D'], ['B']]
[['C'], ['B', 'A', 'D']]
[['D'], ['C'], ['B'], ['A']]
[['D'], ['C'], ['A'], ['B']]
[['D'], ['C'], ['B', 'A']]
[['D'], ['B'], ['C'], ['A']]
[['D'], ['B'], ['A'], ['C']]
[['D'], ['B'], ['C', 'A']]
[['D'], ['A'], ['C'], ['B']]
[['D'], ['A'], ['B'], ['C']]
[['D'], ['A'], ['C', 'B']]
[['D'], ['C', 'B'], ['A']]
[['D'], ['C', 'A'], ['B']]
[['D'], ['B', 'A'], ['C']]
[['D'], ['C', 'B', 'A']]
[['A', 'B'], ['C'], ['D']]
[['A', 'B'], ['D'], ['C']]
[['A', 'B'], ['C', 'D']]
[['A', 'C'], ['B'], ['D']]
[['A', 'C'], ['D'], ['B']]
[['A', 'C'], ['B', 'D']]
[['A', 'D'], ['C'], ['B']]
[['A', 'D'], ['B'], ['C']]
[['A', 'D'], ['C', 'B']]
[['B', 'C'], ['A'], ['D']]
[['B', 'C'], ['D'], ['A']]
[['B', 'C'], ['A', 'D']]
[['B', 'D'], ['C'], ['A']]
[['B', 'D'], ['A'], ['C']]
[['B', 'D'], ['C', 'A']]
[['C', 'D'], ['B'], ['A']]
[['C', 'D'], ['A'], ['B']]
[['C', 'D'], ['B', 'A']]
[['A', 'B', 'C'], ['D']]
[['A', 'B', 'D'], ['C']]
[['A', 'C', 'D'], ['B']]
[['B', 'C', 'D'], ['A']]
[['A', 'B', 'C', 'D']]

关于Python:列表内容的所有可能组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61530864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com