【发布时间】:2021-02-01 03:27:12
【问题描述】:
我有两个字典(a、b)。
每个都有许多具有不同长度列表的键。每个列表都有多个元组。
如果对应列表有任何与b 共享的元组,不管b 的键值如何,是否有任何快速的方法来获取a 的键?
示例如下:
b = {41: [(37, 15), (36, 17), (36, 16), (35, 16), (36, 15)],
42: [(40, 15), (44, 15), (40, 18), (44, 16)]}
a = {38: [(36, 18), (37, 15), (35, 16)],
39: [(57, 0), (54, 0), (55, 0), (56, 0)]}
def test_overlap(a, b):
overlap = frozenset(b).isdisjoint(a)
return not overlap
list_remove = []
for id_b, b_i in b.items():
for id_a, a_i in a.items():
if test_overlap(a[id_a], b[id_b]):
list_remove.append(id_a)
list_remove=list(set(list_remove))
print(list_remove)
# Output: [38]
【问题讨论】:
标签: python list dictionary tuples intersection