I am trying to count the ip addresses found in the log file on two servers and then combine the dictionary statistics together without losing any items or counts. I found a partial solution in another question, but as you can see, it falls by a couple '10.10.0.1':7.
>>> a = {'192.168.1.21':23,'127.0.0.1':5,'12.12.12.12':5,'55.55.55.55':10}
>>> b = {'192.168.1.21':27,'10.10.0.1':7,'127.0.0.1':1}
>>> c = {}
>>> for elem in a:
... c[elem] = b.get(elem, 0) + a[elem]
...
>>> print c
{'55.55.55.55': 10, '12.12.12.12': 5, '127.0.0.1': 6, '192.168.1.21': 50}
The accounts come together, but if the key does not exist in dict a, it is discarded. I'm having trouble figuring out the last bit of logic ... maybe one more for an element in b: if a.get (elem, 0) exists: pass else, add it to c?
user221014
source
share