How to cut very deep json or dictionary in Python?

I have a json object that is very deep. In other words, I have a dictionary containing dictionaries containing dictionaries, etc. Many times. Thus, it can be imagined as a huge tree in which some nodes are very far from the root of the node.

Now I would like to cut this tree so that I have only nodes in it, separated by no more than N steps from the root. Is there an easy way to do this?

For example, if I have:

{'a':{'d':{'e':'f', 'l':'m'}}, 'b':'c', 'w':{'x':{'z':'y'}}}

And I want to save only nodes that are 2 steps from the root, I should get:

{'a':{'d':'o1'}, 'b':'c', 'w':{'x':'o2'}}

So, I just replace the far-standing dictionaries with individual meanings.

+4
source share
3

, , . , :

import collections

def cut(dict_, maxdepth, replaced_with=None):
    """Cuts the dictionary at the specified depth.

    If maxdepth is n, then only n levels of keys are kept.
    """
    queue = collections.deque([(dict_, 0)])

    # invariant: every entry in the queue is a dictionary
    while queue:
        parent, depth = queue.popleft()
        for key, child in parent.items():
            if isinstance(child, dict):
                if depth == maxdepth - 1:
                    parent[key] = replaced_with
                else:
                    queue.append((child, depth+1))
+4
def prune(tree, max, current=0):
    for key, value in tree.items():
        if isinstance(value, dict):
            if current == max:
                tree[key] = None
            else:
                prune(value, max, current + 1)

, , , . . :.

>>> dic = {'a':{'d':{'e':'f', 'l':'m'}}, 'b':'c', 'w':{'x':{'z':'y'}}}
>>> prune(dic, 1)
>>> dic
{'b': 'c', 'w': {'x': None}, 'a': {'d': None}}
+2

You can do something like:

initial_dict = {'a':{'d':{'e':'f', 'l':'m'}}, 'b':'c', 'w':{'x':{'z':'y'}}}
current_index = 0
for item in initial_dict.items():
    if isinstance(item[1], dict):
        current_index += 1
        initial_dict[item[0]] = {key:'o'+str(current_index) for key in item[1].keys()}

I believe that one problem with this code is that for several second-level keywords (an example follows) you will get the same value, but you can adapt the code for it to work.

For example:.

# suppose you have this dict initially
initial_dict = {'a':{'d':{'e':'f', 'l':'m'}}, 'b':'c', 'w':{'x':{'z':'y'}, 'b':{'p':'r'}}}
# you would get
initial_dict = {'a':{'d':'o1'}}, 'b':'c', 'w':{'x':'o2', 'b':'o2'}}
-2
source

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


All Articles