现在我有两个不同大小的元组列表,如下所示:
a = [('NC', 0, 'Eyes'),('NC', 3, 'organs'),('NC', 19, 'neurons'),...]
b = [(0, 'Hypernym', 3),(19, 'Holonym', 0),...]上面列表中的常见值是int数字,预期的结果应该如下所示:
result = [
{'s_type':'NC', 's':'Eyes', 'predicate':'Hypernym', 'o_type':'NC', 'o':'organs'},
{'s_type':'NC', 's':'neurons', 'predicate':'Holonym', 'o_type':'NC', 'o':'Eyes'},
...]我已经将上面两个列表转换成字典,并尝试嵌套循环,但无法获得此输出。有没有人能帮帮我?
发布于 2021-06-28 06:32:27
我设法让它工作起来了。如果有任何其他需要修复的细节,请告诉我。
a = [('NC', 0, 'Eyes'), ('NC', 3, 'organs'), ('NC', 19, 'neurons')]
b = [(0, 'Hypernym', 3), (19, 'Holonym', 0)]
result = []
for s_type, common, s in a:
related = list(filter(lambda x: x[0] == common, b))
for o_type, predicate, next in related:
next_related = list(filter(lambda x: x[1] == next, a))
for s_type, _, organ in next_related:
result.append({'s_type': s_type, 's': s,
'predicate': predicate, 'o_type': o_type, 'o': organ})
print(result)我希望这就是你要找的。有许多其他方法可以做到这一点,但根据您对问题的描述,这应该是可行的。
https://stackoverflow.com/questions/68155721
复制相似问题