Python dictionary doesn't work as expected with dictionaries deeper than 1

Here is an example illustrating the problem ...

a = {
    "foo" : 2,
    "bar" : 3,
}

b = {
    "bar" : 4,
    "zzz" : 5,
}

print(json.dumps(dict(a, **b), indent=4))

This gives you the following result ...

{
    "foo": 2,
    "bar": 4,
    "zzz": 5
}

Notice how the key "foo"from was added to the result a?

Now look at this example ...

a = {
    "foo" : {
        "1" : {
            "foo" : True,
        },
        "2" : {
            "foo" : True,
        },
        "3" : {
            "foo" : True,
        },
        "4" : {
            "foo" : True
        }
    }
}

b = {
    "foo" : {
        "1" : {
            "foo" : True,
        },
        "2" : {
            "foo" : True,
        },
        "3" : {
            "foo" : False,
        }
    }
}

print(json.dumps(dict(a, **b), indent=4))

This gives you the following result ...

{
    "foo": {
        "1": {
            "foo": true
        },
        "3": {
            "foo": false
        },
        "2": {
            "foo": true
        }
    }
}

"3"It was updated in the same way as it "bar"was updated in the previous example, but note that the key "4"from was anot added to the result, for example, as it "foo"was in the previous example?

So my expected result:

{
    "foo": {
        "1": {
            "foo": true
        },
        "3": {
            "foo": false
        },
        "2": {
            "foo": true
        },
        "4": {
            "foo": true
        }
    }
}

How can I modify this dictionary combining process to store keys in awhich are not in b?

My common goal is to have a dictionary a, but with any values ​​that are boverridden.

+4
2

. , , **kwargs.

, **b dict(), , dict(a, bar=4, zzz=5). {zzz: 5}, {bar: 4} .

, **b dict(), , dict(a, foo={...}). foo a, , b. , update, .

:

a = {
    "foo" : {
        "1" : {
            "foo" : True,
        },
        "2" : {
            "foo" : True,
        },
        "3" : {
            "foo" : True,
        },
        "4" : {
            "foo" : True
        }
    }
}

b = {
    "foo" : {
        "1" : {
            "foo" : True,
        },
        "2" : {
            "foo" : True,
        },
        "3" : {
            "foo" : False,
        },
        "5" : {
            "foo" : False,
        }
    }
}

print(json.dumps(dict(a, **b), indent=4))

output:
{
    "foo": {
        "1": {
            "foo": true
        },
        "2": {
            "foo": true
        },
        "3": {
            "foo": false
        },
        "5": {
            "foo": false
        }
    }
}
+2

, dict, dict.update() . , , , dict(a, **b) a b b, , a "foo" b "foo"

import json

for k in a.keys():
    try:
        a[k].update(b[k])
    except KeyError:
        continue

print(json.dumps(a, indent=4))

, "1" ,"2" ,"3" dict b "4" dict a. dict n , . , :

:

a = {
    "foo" : {
        "1" : {
            "foo" : {"1": True,
                     "2": False},
        },
        "2" : {
            "foo" : True,
        },
        "3" : {
            "foo" : True,
        },
        "4" : {
            "foo" : True
        }
    },
}

b = {
    "foo" : {
        "1" : {
            "foo" : {"1": True,
                     "3": True},
        },
        "2" : {
            "foo" : True,
        },
        "3" : {
            "foo" : False,
        }
    }
}

:

import json

def update(dctA, dctB):
    if isinstance(dctA, dict) and isinstance(dctB, dict):
        for k in set(dctA.keys()) & set(dctB.keys()):
            update(dctA[k], dctB[k])
            try:
                dctA[k].update(dctB[k])
                dctB.pop(k)
            except (KeyError, AttributeError):
                continue
    return dctA

print(json.dumps(update(a, b), indent=4))

:

{
    "foo": {
        "1": {
            "foo": {
                "1": true,
                "2": false,
                "3": true
            }
        },
        "2": {
            "foo": true
        },
        "3": {
            "foo": false
        },
        "4": {
            "foo": true
        }
    }
}

, "3" "foo" false, "4" dict b "4". , , "foo" , {"1": True, "2": False} {"1": True, "3": True} {"1": true, "2": false, "3": true}. "1" , "2" , dict b "2", "3" , dict b.

+1

Source: https://habr.com/ru/post/1686169/


All Articles