如何根据配额生成所有可能的排列?

How to generate all the possible permutations based on a quota?

给定两个列表 XY:

X = ['A', 'B', 'C', 'D']

Y = ['I', 'J', 'K', 'L', 'M', 'N']

如何在考虑至少 XY 中的至少 2 个元素的情况下创建所有可能的 4 种组合。例如,有效组合如下所示:

[('A', 'J', 'M', 'D'), ..., ('I', 'M', 'B', 'C')]

原因是它包含 XY 中的 2 个元素。组合 [('A', 'B', 'M', 'D')] 无效,因为它包含来自 X 的 3 个元素和来自 Y 的 1 个元素。到目前为止,我尝试:

import itertools
set(list(itertools.permutations([e for e in zip(X, Y)])))

但是,这违反了配额限制。实施配额排列的正确方法是什么?

您只需要对每个列表中的 2 个进行排列并将它们组合起来,然后对每组 4 个进行所有组合:

from itertools import permutations, combinations
X = ['A', 'B', 'C', 'D']
Y = ['I', 'J', 'K', 'L', 'M', 'N']

x = (n+m for n in permutations(X,2) for m in permutations(Y,2))
for i in x:
    print(list(combinations(i,4)))