如何获取两个列表之间的所有映射?

时间:2014-04-14 10:42:26

标签: python list combinatorics itertools

我们有两个名单,A和B:

A = ['a','b','c']
B = [1, 2]

是否有一种pythonic方法来构建包含2 ^ n的A和B之间的所有映射集(此处为2 ^ 3 = 8)?那就是:

[(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)]

使用itertools.product,可以获得所有元组:

import itertools as it
P = it.product(A, B)
[p for p in P]

给出了:

Out[3]: [('a', 1), ('a', 2), ('b', 1), ('b', 2), ('c', 1), ('c', 2)]

2 个答案:

答案 0 :(得分:24)

您可以使用itertools.productzip

执行此操作
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)]]

product(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压缩它,以获得所需的输出。

答案 1 :(得分:11)

import itertools as it

A = ['a','b','c']
B = [1, 2]

for i in it.product(*([B]*len(A))):
    print(list(zip(A, i)))

输出:

[('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)]

不确定它是否非常pythonic,如果你看it.product(*([B]*len(A))),因为它使用了多个特定于python的语言功能。但它实际上太神秘了,不能成为pythonic ...... B根据A的长度重复n次并解压缩到产品函数。