简体   繁体   中英

Nested lists and dictionaries in python

I have 3 different dictionaries in python like these

d1 = {'simple_key':'hello'}
d2 = {'k1':{'k2':'hello'}}
d3 = {'k1':[{'nest_key':['this is deep',['hello']]}]}

and I want to grab the word 'hello' from these dictionaries. But the problem I am facing is that I want to find a generic way to extract it. How can I do it?

To get the key path in the dict using value, you could flatten it as json.

>>> from json_flatten import flatten
>>> d1 = {'simple_key':'hello'}
>>> d2 = {'k1':{'k2':'hello'}}
>>> d3 = {'k1':[{'nest_key':['this is deep',['hello']]}]}
>>> flatten(d2)
{'k1.k2': 'hello'}
>>> flat = flatten(d3)
{'k1.0.nest_key.0': 'this is deep', 'k1.0.nest_key.1.0': 'hello'}

To find the matching keys, use,

>>> [k for k, v in flat.items() if v == 'hello']
['k1.0.nest_key.1.0']

JSON Flatten

You can keep going inside the dictionary until you are at a value, not a dictionary. Recursion would be helpful here:

def get_value(dict):
     dict_value = list(dict.values())[0]
     if type(dict_value) is dict:
          get_value(dict_value)
     else: 
          return dict_value

there is probably a nicer way to do dict_value = list(dict.values())[0] but not coming to mind right now

You could make your own flattener function which yields the values in a dict and members of a list:

def is_nonstring_iterable(x):
    return ((hasattr(x, "__iter__") or hasattr(x, "__getitem__")) 
        and not isinstance(x, str))

def flatten(thing):
    """ Recursively iterate through values in dictionary-like object or members 
        in lists, tuples or other non-string iterables """
    if hasattr(thing, "values"):
        thing = thing.values()
    if is_nonstring_iterable(thing):
        for element in thing:
            yield from flatten(element)
    else:
        yield thing

for d in [d1, d2, d3]:
    print(list(flatten(d)))
    print(any(item == "hello" for item in flatten(d))) # returns whether hello is found

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