【发布时间】:2013-05-03 19:39:47
【问题描述】:
我有两个列表列表,基本上需要根据它们的匹配项(列表)相互映射。输出是映射对的列表。当要映射的列表长度为 1 时,我们可以在另一个列表中查找直接匹配项。问题出现了,当要映射的列表长度 > 1 时,如果 A 中的列表是 B 的子集,我需要在其中找到。
输入:
A = [['point'], ['point', 'floating']]
B = [['floating', 'undefined', 'point'], ['point']]
我失败的代码:
C = []
for a in A:
for b in B:
if a == b:
C.append([a, b])
else:
if set(a).intersection(b):
C.append([a, b])
print C
预期输出:
C = [
[['point'], ['point']],
[['point', 'floating'], ['floating', 'undefined', 'point']]
]
【问题讨论】:
-
为什么
[['point', 'floating'],['point']]不在预期输出中? -
@ashwini 参见上面的解释。我已经解释过了。
-
length>2(您现在已更新)困扰着我,请参阅下面的解决方案。 -
@AshwiniChaudhary 对不起!那是一个错字。
-
为什么
[['point'], ['floating', 'undefined', 'point']]不在预期的输出中?
标签: python list python-2.7 python-3.x