按键和返回重复比较两个词典列表

时间:2013-11-20 21:18:12

标签: 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', '45', 'vodka', '9', 'vinegar', '7', 'beer', '6', 'alcohol', '5', 'whiskey', '5'] }

dic2 = {'popped ': ['question', '30', 'balloon', '18', 'zit', '10', 'popcorn', '6', 'pimple', '6'] 
'planted ': ['flower', '28', 'tree', '18', 'seed', '9', 'vegetable', '4', 'bush', '3', 'grass', '3'], 'aaron distilled  ': ['water', '14', 'vinegar', '9', 'wine', '8', 'alcohol', '8']}

我想比较两者,找出两者中重复的值,以便我的结果如下:

dic3 = {'popped ': ['balloon', 'question', 'popcorn'], 
'planted ': ['flower', 'tree', 'seed'], 
'distilled  ': ['water', 'vinegar','alcohol']}

我尝试了一些不同的东西,但我没有接近成功。您对每个方向有什么建议吗?我非常感谢你!

1 个答案:

答案 0 :(得分:1)

dic3=dict()

    for key, val in dic1.items():
        val2=dic2[key]

        val3=set(val).intersection(set(val2))
        dic3[key]=val3