Recursive dictionary modification in python

What would be the easiest way to rotate this dictionary:

{'item':{'w':{'c':1, 'd':2}, 'x':120, 'y':240, 'z':{'a':100, 'b':200}}}

in that:

{'item':{'y':240, 'z':{'b':200}}}

given just what you need vars y and b, keeping the dictionary structure? The size or number of elements or the depth of the dictionary should not matter, since the one I work with can be anywhere from 2 to 5 levels.

EDIT: I apologize for the type earlier and clarify, I am given an array of strings (for example, ['y', 'b']) that I need to find in the dictionary, and then save ONLY 'y' and "b", as well as any other keys to save the structure of the original dictionary, in this case it will be "z"

The best example can be found here where I need the chipset model, VRAM and resolution.

Regarding the comment, the input will be the link above as the start dictionary along with the array ['chipset model', 'vram', 'resolution'] as the storage list. He should return this:

{'Graphics/Displays':{'NVIDIA GeForce 7300 GT':{'Chipset Model':'NVIDIA GeForce 7300 GT', 'Displays':{'Resolution':'1440 x 900 @ 75 Hz'}, 'VRAM (Total)':'256 Mb'}}
+4
source share
4 answers

Assuming the dictionary you want to assign to the super dictionary element foo, you could just do this:

my_dictionary['keys']['to']['subdict']=foo

As for your editing, where you need to eliminate all keys, except those that are on a specific list, this function should do the trick:

def drop_keys(recursive_dict,keep_list):
    key_list=recursive_dict.keys()
    for key in key_list:
        if(type(recursive_dict[key]) is dict):
            drop_keys(recursive_dict[key], keep_list)
        elif(key not in keep_list):
            del recursive_dict[key]
+1
source

Something like that?

d = {'item': {'w': {'c': 1, 'd': 2}, 'x': 120, 'y': 240, 'z': {'a': 100, 'b': 200}}}
l = ['y', 'z']
def do_dict(d, l):
    return {k: v for k, v in d['item'].items() if k in l}
+1
source

, , @Dan :

def recursive_del(d,keep):
    for k in d.copy():
        if type(d[k]) == dict:
            recursive_del(d[k],keep)
            if len(d[k]) == 0: #all keys were deleted, clean up empty dict
                del d[k]
        elif k not in keep:
            del d[k]

:

>>> keepset = {'y','b'}
>>> a = {'item':{'w':{'c':1, 'd':2}, 'x':120, 'y':240, 'z':{'a':100, 'b':200}}}
>>> recursive_del(a,keepset)
>>> a
{'item': {'z': {'b': 200}, 'y': 240}}

, , , , , ; 'w':{} .

+1

, - , , . , dict.

" , ".

def rule2(key, value):
    if key == 'VRAM (Total)':
        return (key, value)
    elif key == 'Chipset Model':
        return (key, value)

def rule1(key, value):
    if key == "Graphics/Displays":
        if isinstance(value, dict):
            return (key, recursive_checker(value, rule1))
        else:
            return (key, value)
    else:
        return (key, recursive_checker(value, rule2))

def recursive_checker(dat, rule):

    def inner(item):
        key = item[0]
        value = item[1]

        return rule(key, value)

    return dict(filter(lambda x: x!= None, map(inner, dat.items())))

# Important bits    
print recursive_checker(data, rule1)

, , , , , , , . . .

0

All Articles