您可以使用
itertools.product和
zip
from itertools import product
print [zip(A, item) for item in product(B, repeat=len(A))]
产量
[[('a', 1), ('b', 1), ('c', 1)],
[('a', 1), ('b', 1), ('c', 2)],
[('a', 1), ('b', 2), ('c', 1)],
[('a', 1), ('b', 2), ('c', 2)],
[('a', 2), ('b', 1), ('c', 1)],
[('a', 2), ('b', 1), ('c', 2)],
[('a', 2), ('b', 2), ('c', 1)],
[('a', 2), ('b', 2), ('c', 2)]]
产品(B,repeat = len(A))产生
[(1, 1, 1),
(1, 1, 2),
(1, 2, 1),
(1, 2, 2),
(2, 1, 1),
(2, 1, 2),
(2, 2, 1),
(2, 2, 2)]
然后我们从产品中挑选每个元素,并用A压缩,以获得所需的输出。