Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.6合并字典失败_Python_Python 3.x_Dictionary - Fatal编程技术网

Python 3.6合并字典失败

Python 3.6合并字典失败,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我正在尝试合并两个词典,在搜索有关堆栈溢出的一个封闭问题后,我找到了下一个解决方案: 但这不起作用。虽然我知道我的代码是正确的,因为我观察到了单个字典的正确结果,但一旦我合并,就不会得到正确的结果 def readFiles(path1): // count words if __name__ == '__main__': a = readFiles('C:/University/learnPy/dir') b = readFiles('C:/Users/user/

我正在尝试合并两个词典,在搜索有关堆栈溢出的一个封闭问题后,我找到了下一个解决方案:

但这不起作用。虽然我知道我的代码是正确的,因为我观察到了单个字典的正确结果,但一旦我合并,就不会得到正确的结果

def readFiles(path1):
    // count words


if __name__ == '__main__':
    a = readFiles('C:/University/learnPy/dir')
    b = readFiles('C:/Users/user/Anaconda3/dir')
    bigdict = {**a, **b}
    print(a['wee'])
    print(b['wee'])
    print(bigdict['wee'])
a
中有一个
.txt
文件包含
2个wee

b
中有一个
.txt
文件,其中包含
1wee


所以我希望bigdict的输出是3,但我观察到bigdict只是得到第一个dict的数字。
{**dict1(这一个),**dict2}
,合并不起作用。

问题:出了什么问题?为什么在Python3.6上,当答案表明它应该工作时,它会失败。

dict(**x,**y)
是。通过用第二个参数覆盖第一个参数的值来创建
bigdict
。你需要自己对这些值求和

您可以使用
计数器

from collections import Counter
a = {'wee':1, 'woo':2 }
b = {'wee':10, 'woo': 20 }
bigdict = dict(Counter(a)+Counter(b))

Out[23]: {'wee': 11, 'woo': 22}

谢谢你的回答+链接。帮助很大。
from collections import Counter
a = {'wee':1, 'woo':2 }
b = {'wee':10, 'woo': 20 }
bigdict = dict(Counter(a)+Counter(b))

Out[23]: {'wee': 11, 'woo': 22}