1

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))
Soviut
  • 88,194
  • 49
  • 192
  • 260
stingray
  • 241
  • 2
  • 5
  • 1
    How are the sub-dictionaries created? Because this looks like "ye old repeated reference to the same object problem", i.e. every `d[i]` refers to the same dictionary object. – dhke Aug 06 '17 at 17:48
  • 1
    So can i update them if i am not looping through the dict? Say i create 2 lists with the keys `k1=['Aug 1 04:01:0','Aug 1 04:06:0' ], k2=['rsyslogd','systemd']` can i update the dict using `d[k1[0]][k2[0] = ` ? – stingray Aug 06 '17 at 17:59
  • 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) u = dict.fromkeys(uniq_time, dict.fromkeys(uniq_processes, 0))` – stingray Aug 06 '17 at 18:12
  • Please take a look at https://stackoverflow.com/questions/29116226/why-does-updating-one-dictionary-object-affect-other Also, you may find this article helpful: [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), which was written by SO veteran Ned Batchelder. – PM 2Ring Aug 06 '17 at 18:16
  • your key is 'Aug 1 05:01:**0**'. while your content is 'Aug 1 05:01:**01**'. Do you expect them to match? – LxL Aug 06 '17 at 18:17
  • I am compiling a regex `regex = re.compile(i + '.*.' + x + '.*')`. I am certain that matching works. I've tested it. – stingray Aug 06 '17 at 18:22
  • What version of python are you running? I ran your code in python 3.6 and here is my result https://i.stack.imgur.com/m0SqH.png – LxL Aug 06 '17 at 18:42
  • I am using 3.6 as well. What editor is that? – stingray Aug 06 '17 at 19:48

0 Answers0