开发者

Subtract dict A from dict B (deep del)?

开发者 https://www.devze.com 2022-12-18 01:16 出处:网络
If I have a deeply nested dict is there a built-in way to subtract/remove list of \"paths\" (eg: 开发者_运维知识库keyA.keyB.key1, keyA.keyC.key2, etc) or a the keys of a second dict from the original

If I have a deeply nested dict is there a built-in way to subtract/remove list of "paths" (eg: 开发者_运维知识库keyA.keyB.key1, keyA.keyC.key2, etc) or a the keys of a second dict from the original dict? Or maybe there is a common module which has functionality like this?


Here's a suggestion:

D = { "keyA": { 
        "keyB" : {
            "keyC" : 42,
            "keyD": 13
            },
        "keyE" : 55
        }
    }

def remove_path(dictionary, path):
    for node in path[:-1]:
        dictionary = dictionary[node]
    del dictionary[path[-1]]

remove_path(D, ["keyA", "keyB", "keyD"])

print D # prints {'keyA': {'keyB': {'keyC': 42}, 'keyE': 55}}

You'll probably want to introduce some error checking, too.


Just in case the other answers aren't what you're looking for, here's one that subtracts one dictionary from another.

def subtract(a, b):
    """ Remove the keys in b from a. """
    for k in b:
        if k in a:
            if isinstance(b[k], dict):
                subtract(a[k], b[k])
            else:
                del a[k]


Another solution:

d = {
    'A' : {
        'C' : {
            'D' : {
                'E' : 4,
            },
            'F' : 5,
        },
    },
    'B' : 2,
}

def DeepDictDel(path, dict):
    for key in path.split('.'):
        owner = dict
        dict = dict[key]

    del owner[key]


print d # prints {'A': {'C': {'D': {'E': 4}, 'F': 5}}, 'B': 2}
DeepDictDel('A.C.D', d)
print d # prints {'A': {'C': {'F': 5}}, 'B': 2}
0

精彩评论

暂无评论...
验证码 换一张
取 消