-1

I tried finding many places , but could not find same one, please help in this python code. I have three nested multi level dictionaries, two main (A and B) and one small (temp)

A={"X":{"XX":1,"XX1":56},"Y":{"YY":2},"Z":{"ZZ":{"ZA":34,"ZB":35}}

B={"X":{"XX":2,"XX1":34},"L":{"LL":3},"Z":{"ZZ":{"ZA":31,"ZB":38}}

temp={"X":{"XX":1},"Z":{"ZZ":{"ZA":34}}

If values of "temp" matches "A" then update "A" and "B" by deleting that entire record. So, final A and B should be

A={"X":{"XX1":56},"Y":{"YY":2},"Z":{"ZZ":{"ZB":35}}
B={"X":{"XX1":34},"L":{"LL":3},"Z":{"ZZ":{"ZB":38}}
Saman
  • 33
  • 1
  • 7

2 Answers2

0

Iterate over temp, check if the value matches what's in A, delete (by key) from A and B if so.

>>> A={"X":{"XX":1},"Y":{"YY":2},"Z":{"ZZ":3}}
>>> 
>>> B={"X":{"XX":2},"L":{"LL":3},"Z":{"ZZ":7}}
>>> 
>>> temp={"X":{"XX":1},"Z":{"ZZ":5}}
>>> for k, v in temp.items():
...     if A[k] == v:
...         del A[k]
...         del B[k]
... 
>>> A
{'Y': {'YY': 2}, 'Z': {'ZZ': 3}}
>>> B
{'L': {'LL': 3}, 'Z': {'ZZ': 7}}
Samwise
  • 68,105
  • 3
  • 30
  • 44
  • Thanks @Samwise, but I think this would not work if the list if two three levels deep nested. – Saman Jul 25 '22 at 15:31
  • @Saman You need to change your question to explain better what you need. There is no mention of multiple "levels" in your original question – DarkKnight Jul 25 '22 at 15:32
  • @Samwise, I updated the question for more clarity, thanks for highlighting. – Saman Jul 25 '22 at 15:45
  • @Saman Your updated code doesn't run (looks like you didn't close all the `{}`s in your haste to update the question) so I can't just copy and paste the new input data in, but I don't see any reason why my answer wouldn't work with nested dicts, since the equality comparison handles nested dicts just fine. – Samwise Jul 25 '22 at 16:12
0

The following code delivers the results you want.

def dig(a, b, c=None):
    pop_keys = []
    keys_b = b.keys()
    for key_a in a.keys():
        if key_a in keys_b:
            # checks if you have to dive deeper
            if isinstance(a[key_a], dict) and isinstance(b[key_a], dict):
                dig(a[key_a], b[key_a], c[key_a] if (isinstance(c, dict) and key_a in c.keys()) else None)


                # checks if endnodes are  equal
            if a[key_a] == b[key_a]:
                pop_keys.append(key_a)
                    # removing cant be done here because this would interfere with the loop
    # removes the keys that are equal
    for key_a in pop_keys:
        a.pop(key_a, None)
        b.pop(key_a, None)
        if isinstance(c, dict) and key_a in c.keys():
            c.pop(key_a, None)


dig(A, temp_, B)
print(A)
print(B)
print(temp_)

Input:

A = {"X": {"XX": 1, "XX1": 56, "XX2": {"XXX": 1}}, "Y": {"YY": 2}, "Z": {"ZZ": {"ZA": 34, "ZB": 35}}}

B = {"X": {"XX": 2, "XX1": 34}, "L": {"LL": 3}, "Z": {"ZZ": {"ZA": 31, "ZB": 38}}}

temp_ = {"X": {"XX": 1, "XX2": {"XXX": 1}}, "Z": {"ZZ": {"ZA": 34}}}

Output:

{'X': {'XX1': 56}, 'Y': {'YY': 2}, 'Z': {'ZZ': {'ZB': 35}}}
{'X': {'XX1': 34}, 'L': {'LL': 3}, 'Z': {'ZZ': {'ZB': 38}}}
{'X': {}, 'Z': {'ZZ': {}}}
Moritz Hartmann
  • 140
  • 1
  • 10