如何以自定义格式打印两个列表的所有排列?

How to print all the permutations of two lists in a custom format?

P = [(2, 2, 3), (2, 3, 2), (3, 2, 2)]

X = [[(1, 2, 4), (1, 4, 2), (2, 1, 4), (2, 4, 1), (4, 1, 2), (4, 2, 1)], [(1, 2, 6), (1, 6, 2), (2, 1, 6), (2, 6, 1), (6, 1, 2), (6, 2, 1)], [(1, 3, 6), (1, 6, 3), (3, 1, 6), (3, 6, 1), (6, 1, 3), (6, 3, 1)]]

如何以以下形式打印上述 2 个列表的所有排列:

P1(X1) P2(x2) P3(x3)

例如:

2(1) 2(2) 3(4),
2(1) 2(4) 3(2),
2(2) 2(1) 3(4) and so on

即P 的第一个元组与 X 的所有元组组合,然后 P 的第二个元组与 X 的所有元组组合,依此类推。

您可以这样使用 itertools.product and zip

from itertools import product

P = [(2, 2, 3), (2, 3, 2), (3, 2, 2)]

X = [[(1, 2, 4), (1, 4, 2), (2, 1, 4), (2, 4, 1), (4, 1, 2), (4, 2, 1)],
     [(1, 2, 6), (1, 6, 2), (2, 1, 6), (2, 6, 1), (6, 1, 2), (6, 2, 1)],
     [(1, 3, 6), (1, 6, 3), (3, 1, 6), (3, 6, 1), (6, 1, 3), (6, 3, 1)]]

for p, xs in product(P, X):
    for x in xs:
        print(" ".join(f"{a}({b})" for a, b in zip(p, x)))

这个输出

2(1) 2(2) 3(4)
2(1) 2(4) 3(2)
2(2) 2(1) 3(4)
2(2) 2(4) 3(1)
2(4) 2(1) 3(2)
2(4) 2(2) 3(1)
2(1) 2(2) 3(6)
2(1) 2(6) 3(2)
2(2) 2(1) 3(6)
...