I am trying to update values of sub dicts when there is a match. For some reason i can't understand at the end all sub dicts have same values.
def (d,content):
for i in d:
for x in d[i]:
regex = re.compile(i + '.*.' + x + '.*')
for string in content:
if (bool(regex.match(string))) == True:
d[i][x] +=1
print("match")
I just started learning Python. I couldn't understand the logic in answers for question Update value of a nested dictionary of varying depth.
Below is how the input dict looks like.
{'Aug 1 03:46:0':
{'chronyd': 0, 'rsyslogd': 0, 'systemd': 0},
'Aug 1 03:56:4':
{'chronyd': 0, 'rsyslogd': 0, 'systemd': 0},
'Aug 1 04:01:0':
{'chronyd': 0, 'rsyslogd': 0, 'systemd': 0},
'Aug 1 04:06:0':
{'chronyd': 0, 'rsyslogd': 0, 'systemd': 0},
'Aug 1 05:01:0':
{'chronyd': 0, 'rsyslogd': 0, 'systemd': 0}}
.
content='''
Aug 1 03:46:01 localhost rsyslogd: [origin software="rsyslogd" swVersion="7.4.7" x-pid="880" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
Aug 1 03:56:48 localhost chronyd[640]: Source 96.244.96.19 replaced with 96.126.105.86
Aug 1 04:01:01 localhost systemd: Started Session 7 of user root.
Aug 1 04:01:01 localhost systemd: Starting Session 7 of user root.
Aug 1 04:06:01 localhost systemd: Removed slice user-0.slice.
Aug 1 04:06:01 localhost systemd: Stopping user-0.slice.
Aug 1 05:01:01 localhost systemd: Created slice user-0.slice.
Aug 1 05:01:01 localhost systemd: Starting user-0.slice.
Aug 1 05:01:01 localhost systemd: Started Session 8 of user root.
Aug 1 05:01:01 localhost systemd: Starting Session 8 of user root.
'''
Here is how i am creating the dict.
uniq_time = sorted(set([" ".join([" ".join([x[0],x[1]]),x[2][:-1]]) for x in mlist]), key=str.lower)
uniq_processes = sorted(set([ x[4].split('[')[0] for x in mlist]), key=str.lower)
d = dict.fromkeys(uniq_time, dict.fromkeys(uniq_processes, 0))