Python 3.6 merge dictionary not working

I am trying to combine two dictionaries, after searching a closed question about stack overflow, I found the following solution:

mergeDicts = {**dict1, **dict2} 

but that will not work. Although I know that my code is fine, as I observe the correct results for one dictionary, as soon as I merge, I do not get the correct results

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

In athere is 1 .txtfile containing 2 wee
In bthere is 1 .txtfile containing 1 wee

Thus, I expect that the output of bigdict will be 3, but what I observe is bigdict, just getting the numbers of the first dict. {**dict1 (THIS ONE), **dict2}, and merging does not work.

Question: what went wrong? why it doesn't work on python 3.6 when the answers claim it should work.

+4
1

dict(**x, **y) , . bigdict, 1- . .

Counter

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}
+4

Source: https://habr.com/ru/post/1674532/


All Articles