【发布时间】:2018-11-15 10:41:47
【问题描述】:
但就我而言,我有两本字典
dict1 = {'foo' : ['val1' , 'val2' , 'val3'] , 'bar' : ['val4' , 'val5']}
dict2 = {'foo' : ['val2', 'val10', 'val11'] , 'bar' : ['val1' , 'val4']}
我要返回的是
dict3 = {'foo' : ['val10', 'val11'] , 'bar' : ['val1']}
反之
dict4 = {'foo' : ['val1', 'val3'] , 'bar' : ['val5']}
其中 dict3 返回键 'foo' 和 'bar' 在 dict2 中获得的值的字典,而 dict4 是键 'foo' 和 'bar' 在 dict2 中丢失的值的字典
我尝试解决的一种方法是:
iterate over both dictionaries then
if key of dict1 == key of dict2
return the values of the key in dict1 and compare with the values in dict2
return the values that aren't in both
as a dictionary of the key and those values
这个想法行不通,而且显然效率很低。我希望有一种更有效的工作方式来做到这一点
【问题讨论】:
标签: python dictionary