Sorting 按键比较两个字典列表并返回重复
我有两个键相同但值不同的字典:Sorting 按键比较两个字典列表并返回重复,sorting,python-3.x,dictionary,Sorting,Python 3.x,Dictionary,我有两个键相同但值不同的字典: dic1 = {'popped': ['question', '30', 'balloon', '18', 'zit', '10', 'popcorn', '6', 'pimple', '6', 'cherry', '5'], 'planted': ['tree', '30', 'seed', '28', 'flower', '20', 'plant', '7', 'bomb', '4', 'garden', '2'], 'distilled ': ['water
dic1 = {'popped': ['question', '30', 'balloon', '18', 'zit', '10', 'popcorn', '6', 'pimple', '6', 'cherry', '5'],
'planted': ['tree', '30', 'seed', '28', 'flower', '20', 'plant', '7', 'bomb', '4', 'garden', '2'],
'distilled ': ['water', '45', 'vodka', '9', 'vinegar', '7', 'beer', '6', 'alcohol', '5', 'whiskey', '5'] }
及
我想比较两者,找出两者中重复的值,这样我的结果会是:
dic3 = {'popped ': ['balloon', 'question', 'popcorn'],
'planted ': ['flower', 'tree', 'seed'],
'distilled ': ['water', 'vinegar','alcohol']}
我尝试了一些不同的事情,但我离成功还差得远。你对每个方向都有什么建议吗?我非常感谢你
dic3=dict()
for key, val in dic1.items():
val2=dic2[key]
val3=set(val).intersection(set(val2))
dic3[key]=val3
dic3=dict()
for key, val in dic1.items():
val2=dic2[key]
val3=set(val).intersection(set(val2))
dic3[key]=val3