Reputation: 771
GOAL:
I am trying to sum the values between dict ct_db and ct_db2 whenever the keys are the same.
Example:
RR-00 == RR-00, so make 14 + 223
Problem:
All dict keys are not being compared, the operation is terminated as soon as the dict is traversed, but since the keys are out of order, the correct calculations are not made.
SCRIPT:
ct_db = {u'AB-00 ': 18, u'RR-00': 14, u'LD-00 ': 56, u'SR-00': 33, u'YT-00 ': 452}
ct_db2 = {u'CD-07 ': 26, u'RR-00': 223, u'LD-00 ': 367, u'SR-00': 257, u'IA-00 ': 11}
not_installed = []
for elemA, elemB in zip (ct_db, ct_db2):
if (elemA == elemB):
print ("MATCH" + elemA + "-" + elemB)
not_installed.append (ct_db [elemA] + ct_db2 [elemB])
else:
print ("NOT MATCH" + elemA + "-" + elemB)
print (not_installed)
OUTPUT:
NOT MATCH LD-00 - CD-07
NOT MATCH SR-00 - LD-00
NOT MATCH AB-00 - RR-00
NOT MATCH RR-00 - IA-00
NOT MATCH YT-00 - SR-00
[]
Upvotes: 1
Views: 169
Reputation: 22776
You only need to iterate over the keys of one of the dicts, and check if each key is in the other dict:
not_installed = []
for elem in ct_db:
if elem in ct_db2:
not_installed.append(ct_db[elem] + ct_db2[elem])
print(not_installed)
Output:
[237, 423, 290]
Upvotes: 5
Reputation: 22324
Using a dictionary comprehension is a neat alternative way.
Given two dictionaries d1
and d2
:
output = {k: d1[k] + d2[k] for k in d1.keys() & d2.keys()}
The expression d1.keys() & d2.keys()
takes advantage of set
intersection available on dict_keys
objects.
Upvotes: 3
Reputation: 843
You can use the intersection property of sets in this case as used in examples here https://howtodoinjava.com/python/dictionary-intersection/
ct_db = {u'AB-00 ': 18, u'RR-00': 14,
u'LD-00 ': 56, u'SR-00': 33, u'YT-00 ': 452}
ct_db2 = {u'CD-07 ': 26, u'RR-00': 223,
u'LD-00 ': 367, u'SR-00': 257, u'IA-00 ': 11}
not_installed = []
ct_set = set(ct_db)
ct_set2 = set(ct_db2)
for name in ct_set.intersection(ct_set2):
#print(name) to see the common keys
not_installed.append(ct_db[name] + ct_db2[name])
print(not_installed)
Upvotes: 3
Reputation: 13589
Here's a way to do this using collections.Counter
:
from collections import Counter
ct_db = {u'AB-00 ': 18, u'RR-00': 14, u'LD-00 ': 56, u'SR-00': 33, u'YT-00 ': 452}
ct_db2 = {u'CD-07 ': 26, u'RR-00': 223, u'LD-00 ': 367, u'SR-00': 257, u'IA-00 ': 11}
counter = collections.Counter(ct_db)
counter += ct_db2
# >>> counter
# Counter({'YT-00 ': 452, 'LD-00 ': 423, 'SR-00': 290, 'RR-00': 237, 'CD-07 ': 26, 'AB-00 ': 18, 'IA-00 ': 11})
# And if you want only the overlapping keys:
overlapping_keys = ct_db.keys() & ct_db2.keys()
counter = {k: v for k, v in counter.items() if k in overlapping_keys}
# >>> counter
# {'RR-00': 237, 'LD-00 ': 423, 'SR-00': 290}
Upvotes: 3
Reputation: 40763
The algorithm is simple: Find the common keys between the two dictionaries and calculate the sum. In Python 3, the dict.keys()
method returns a set-like object, therefore we can use the &
operator to find the intersection between two sets of keys:
ct_db = {u'AB-00 ': 18, u'RR-00': 14, u'LD-00 ': 56, u'SR-00': 33, u'YT-00 ': 452}
ct_db2 = {u'CD-07 ': 26, u'RR-00': 223, u'LD-00 ': 367, u'SR-00': 257, u'IA-00 ': 11}
print(ct_db)
print(ct_db2)
common_keys = ct_db.keys() & ct_db2.keys()
not_installed = {k: ct_db[k] + ct_db2[k] for k in common_keys}
print(not_installed)
Output:
{'AB-00 ': 18, 'RR-00': 14, 'LD-00 ': 56, 'SR-00': 33, 'YT-00 ': 452}
{'CD-07 ': 26, 'RR-00': 223, 'LD-00 ': 367, 'SR-00': 257, 'IA-00 ': 11}
{'SR-00': 290, 'LD-00 ': 423, 'RR-00': 237}
If you are only interested in the sums, then use not_installed.values()
Upvotes: 3