MTG*_*MTG 0 python dictionary textedit word-cloud
我有连接词典的问题.有这么多代码,所以我在示例中显示我的问题是什么.
d1 = {'the':3, 'fine':4, 'word':2}
+
d2 = {'the':2, 'fine':4, 'word':1, 'knight':1, 'orange':1}
+
d3 = {'the':5, 'fine':8, 'word':3, 'sequel':1, 'jimbo':1}
=
finald = {'the':10, 'fine':16, 'word':6, 'knight':1, 'orange':1, 'sequel':1, 'jimbo':1}
Run Code Online (Sandbox Code Playgroud)
它是wordcloud的字数.我不知道如何连接键的值,这对我来说很困惑.请帮忙.最好的祝福
我会用一个Counter来自collections这个.
from collections import Counter
d1 = {'the':3, 'fine':4, 'word':2}
d2 = {'the':2, 'fine':4, 'word':1, 'knight':1, 'orange':1}
d3 = {'the':5, 'fine':8, 'word':3, 'sequel':1, 'jimbo':1}
c = Counter()
for d in (d1, d2, d3):
c.update(d)
print(c)
Run Code Online (Sandbox Code Playgroud)
输出:
Counter({'fine': 16, 'the': 10, 'word': 6, 'orange': 1, 'jimbo': 1, 'sequel': 1, 'knight': 1})
Run Code Online (Sandbox Code Playgroud)