简体   繁体   中英

Python - collect output of recursive function

I have written a function designed to flatten a json. The function works recursively - if a key, value pair's value is not a json, the key,value pair is returned, otherwise, the function is called again on the value:

def flatten_json(j):
    for k, v in j.items():
        if isinstance(v, dict):
            flatten_json(v)
        else:
            yield (k,v)

flat_json = {x[0]: x[1] for x in flatten_json(j)}

The idea being that whenever the function yield sa tuple, it is collected into the flat_json dict. As it stands, nested outputs are ignored - only the top level key, value pairs appear in flat_json.

This is exactly what yield from is for:), to yield elements one by one from calling a generator from within a generator (whether that is a recursive call or not).

try:

def flatten_json(j):
    for k, v in j.items():
        if isinstance(v, dict):
            yield from flatten_json(v)
        else:
            yield (k,v)


j = {'foo': 'bar', 'foo1': 'bar1', 'nest1': {'foo2': 'bar2'}}
flat_json = {x[0]: x[1] for x in flatten_json(j)}
print(flat_json)

Output:

{'foo': 'bar', 'foo1': 'bar1', 'foo2': 'bar2'}

running example here: https://ideone.com/Z5aO9V

Yield it like the normal data:

yield (k, flatten_json(v))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM