I know that there are several similar questions, but my question is completely different and difficult for me. I have two dictionaries:
d1 = {'a': {'b': {'cs': 10}, 'd': {'cs': 20}}} d2 = {'a': {'b': {'cs': 30}, 'd': {'cs': 20}}, 'newa': {'q': {'cs': 50}}}
i.e. d1 has the key 'a' , and d2 has the keys 'a' and 'newa' (in other words, d1 is my old dict and d2 is my new dict).
I want to iterate over these dictionaries so that if the key is the same, check its value (nested dict), for example. when I find the key 'a' in d2 , I will check if there is a 'b' , if so, check the value of 'cs' (changed from 10 to 30 ), if this value is changed, I want to print it.
Otherwise, I want to get the key 'newa' from d2 as the newly added key.
Therefore, after iterating through these 2 dicts, this is the expected result:
"d2" has new key "newa" Value of "cs" is changed from 10 to 30 of key "b" which is of key "a"
I have the following code with me, I am trying to use a lot of loops that do not work, but this is also not a good option, so I am looking to find out if I can get the expected result using the recursive part of the code.
for k, v in d1.iteritems(): for k1, v1 in d2.iteritems(): if k is k1: print k for k2 in v: for k3 in v1: if k2 is k3: print k2, "sub key matched" else: print "sorry no match found"
python comparison dictionary
rkatkam
source share