简体   繁体   English

Python:将字典与列表中的列表合并为值并对其进行计数

[英]Python: merging dictionaries with lists in lists as values and counting them

I am trying to write a program that can merge two dictionaries (TEXT FILES!). 我正在尝试编写一个可以合并两个字典(TEXT FILES!)的程序。 These dictionaries consist of nouns and verbs that have been indexed from different corpora by another program (and then put into a text file). 这些词典由名词和动词组成,这些名词和动词已由另一个程序从不同的语料库中索引(然后放入文本文件中)。 This is the form of these dictionaries: 这是这些字典的形式:

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

As you can see, they're dictionaries, with keys, that have lists in lists for values. 如您所见,它们是带有键的字典,在值列表中具有列表。 Now I'm trying to get the output like this: 现在,我试图获得如下输出:

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

Until now, I have been able to merge dict1 and dict2 like this (in a string): 到目前为止,我已经能够像这样(在字符串中)合并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]]}

I convert this string as a dictionary with the next statement: finaldict = eval(str1) it turns the whole thing into a dictionary, it also says so when I ask for the type of finaldict, but it won't see the statements like [['eat', 1]] as values or anything. 我使用下一条语句将该字符串转换为字典:finaldict = eval(str1)它将整个内容转换为字典,它也表示这样,当我询问finaldict的类型时,却看不到[ ['eat',1]]作为值或其他任何值。 I need this so I can loop over every item and count how many times it appears with which verb. 我需要这个,所以我可以遍历每个项目并计算该动词出现多少次。

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()}

Nothing too fancy just break it out. 没有什么花哨的只是打破它。

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()]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM