2012-06-14 59 views
1

我正在嘗試編寫一個可以合併兩個字典(TEXT FILES!)的程序。這些字典由名詞和動詞組成,這些名詞和動詞由另一個程序從不同語料庫索引(然後放入一個文本文件中)。這是這些字典的形式:Python:將字典與列表中的列表合併爲值並對它們進行計數

dict1 = {'strawberry': [['eat', 1]], 'family-member': [['look up', 1]], 'mall': [['search', 1]]} 
dict2 = {'strawberry': [['eat', 1]], 'family-member': [['lose', 1]], 'ovation': [['receive', 1]], 'mall': [['build', 1]]} 

正如你可以看到,他們的字典,連鍵,即有值列表清單。 現在我想要得到這樣的輸出:

finaldict = {'strawberry': [['eat', 2]], 'family-member': [['look up', 1]['lose',1]], 'mall': [['search', 1]['build', 1]], 'ovation': [['receive', 1]] 

到現在爲止,我已經能夠合併dict1和dict2像這樣(在一個字符串):

{'strawberry': [['eat', 1]], 'family-member': [['look up', 1]], 'mall': [['search', 
1]], 'strawberry': [['eat', 1]], 'family-member': [['lose', 1]], 'ovation': 
[['receive', 1]], 'mall': [['build', 1]]} 

我這個轉換字符串作爲字典與下一個語句: finaldict = eval(str1) 它把整個事情變成一個字典,它也說,所以當我要求的最終裁決的類型,但它不會看到像[[[ '吃',1]]作爲價值觀或任何東西。我需要這個,所以我可以遍歷每個項目,並計算它出現多少次與哪個動詞。

回答

1
from collections import Counter 

dict1 = {'strawberry': [['eat', 1]], 'family-member': [['look up', 1]], 'mall': [['search', 1]]} 
dict2 = {'strawberry': [['eat', 1]], 'family-member': [['lose', 1]], 'ovation': [['receive', 1]], 'mall': [['build', 1]]} 
result = {k: Counter(dict(v)) for k, v in dict1.items()} 
for k, v in dict2.items(): 
    result.setdefault(k, Counter()).update(dict(v)) 

result = {k: [list(x) for x in v.items()] for k, v in result.items()} 
+0

這是什麼python的最低版本? –

+0

2.7或3.1+,在2.7/3.0中添加了詞典解析,並且在2.7/3.1中添加了「collections.Counter」。 –

0

沒什麼太花哨就把它分開。

from collections import defaultdict 

dict1 = {'strawberry': [['eat', 1]], 'family-member': [['look up', 1]], 'mall': [['search', 1]]} 
dict2 = {'strawberry': [['eat', 1]], 'family-member': [['lose', 1]], 'ovation': [['receive', 1]], 'mall': [['build', 1]]} 
keys = set(dict2.keys()).union(dict1.keys()) 

final = {} 
for k in keys: 
    d1val = dict1.get(k, []) 
    d2val = dict2.get(k, []) 

    resd = defaultdict(lambda: 0) 

    for word, count in d1val: 
     resd[word] += count 

    for word, count in d2val: 
     resd[word] += count 

    final[k] = [list(i) for i in resd.items()] 
相關問題