2013-11-20 37 views
0

我有兩個詞典相同的按鍵,但不同的值:比較的關鍵詞典的兩個列表,並返回重複

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']} 

我試過索姆e不同的事情,但我不會接近成功。你對每個方向有什麼建議嗎?我非常感謝你!

回答

1
dic3=dict() 

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

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

謝謝!完善! – user3008918

相關問題