📜  Python|组合两个字典为公共键添加值

📅  最后修改于: 2022-05-13 01:54:37.951000             🧑  作者: Mango

Python|组合两个字典为公共键添加值

给定两个字典,任务是组合字典,以便我们在结果字典中获得公共键的附加值。
例子:

Input: dict1 = {'a': 12, 'for': 25, 'c': 9}
       dict2 = {'Geeks': 100, 'geek': 200, 'for': 300}

Output: {'for': 325, 'Geeks': 100, 'geek': 200}

让我们看看完成任务的一些方法。
方法#1:朴素的方法

Python3
# Python program to combine two dictionary
# adding values for common keys
# initializing two dictionaries
dict1 = {'a': 12, 'for': 25, 'c': 9}
dict2 = {'Geeks': 100, 'geek': 200, 'for': 300}
 
 
# adding the values with common key
for key in dict2:
    if key in dict1:
        dict2[key] = dict2[key] + dict1[key]
    else:
        pass
         
print(dict2)


Python3
# Python program to combine two dictionary
# adding values for common keys
from collections import Counter
 
# initializing two dictionaries
dict1 = {'a': 12, 'for': 25, 'c': 9}
dict2 = {'Geeks': 100, 'geek': 200, 'for': 300}
 
 
# adding the values with common key
         
Cdict = Counter(dict1) + Counter(dict2)
print(Cdict)


Python3
# Python program to combine two dictionary
# adding values for common keys
import itertools
import collections
 
# initializing two dictionaries
dict1 = {'a': 12, 'for': 25, 'c': 9}
dict2 = {'Geeks': 100, 'geek': 200, 'for': 300}
 
# using defaultdict
Cdict = collections.defaultdict(int)
 
# iterating key, val with chain()
for key, val in itertools.chain(dict1.items(), dict2.items()):
    Cdict[key] += val
     
print(dict(Cdict))


Python3
#Here is another way to combine dictionaies and
#sum values of common keys (runs fast):
 
 
from functools import reduce
 
# creating three dictionaries in a list
dict_seq = [
  {'a': 1, 'b': 2, 'c': 3},
  {'a':10, 'b': 20},
  {'b': 100},
]
 
print(reduce(lambda d1,d2: {k: d1.get(k,0)+d2.get(k,0)
for k in set(d1)|set(d2)}, dict_seq))


输出:
{'for': 325, 'Geeks': 100, 'geek': 200}


方法 #2:使用collections.Counter()

Python3

# Python program to combine two dictionary
# adding values for common keys
from collections import Counter
 
# initializing two dictionaries
dict1 = {'a': 12, 'for': 25, 'c': 9}
dict2 = {'Geeks': 100, 'geek': 200, 'for': 300}
 
 
# adding the values with common key
         
Cdict = Counter(dict1) + Counter(dict2)
print(Cdict)
输出:
Counter({'for': 325, 'geek': 200, 'Geeks': 100, 'a': 12, 'c': 9})


方法 #3:使用itertools.chain()

Python3

# Python program to combine two dictionary
# adding values for common keys
import itertools
import collections
 
# initializing two dictionaries
dict1 = {'a': 12, 'for': 25, 'c': 9}
dict2 = {'Geeks': 100, 'geek': 200, 'for': 300}
 
# using defaultdict
Cdict = collections.defaultdict(int)
 
# iterating key, val with chain()
for key, val in itertools.chain(dict1.items(), dict2.items()):
    Cdict[key] += val
     
print(dict(Cdict))
输出:
{'for': 325, 'a': 12, 'geek': 200, 'Geeks': 100, 'c': 9}


方法 #4:使用functools.reducedict 理解

Python3

#Here is another way to combine dictionaies and
#sum values of common keys (runs fast):
 
 
from functools import reduce
 
# creating three dictionaries in a list
dict_seq = [
  {'a': 1, 'b': 2, 'c': 3},
  {'a':10, 'b': 20},
  {'b': 100},
]
 
print(reduce(lambda d1,d2: {k: d1.get(k,0)+d2.get(k,0)
for k in set(d1)|set(d2)}, dict_seq))

输出:

{'a': 11, 'b': 122, 'c': 3}