Accessing nested keys in Python

冷暖自知 提交于 2020-06-09 05:04:18

问题


I have a nested dictionary as below

entry = {
    0: {"Q": 0},
    1: {"W": 2, "E": 3, "N": 5, "S": 4, "Q": 0},
    2: {
        "N": {
            "Q": {"E"}
        }
    },
}

When I try to access only the keys for the key 1, I get the following:

>>> print(entry[1].keys())
dict_keys(['W', 'E', 'N', 'S', 'Q'])

But for key 2 it only returns the top key and not the nested key.

>>> print(entry[2].keys())
dict_keys(['N'])  

Why is it not returning the nested key of the dictionary?


回答1:


keys()doesn't work that way.

keys()

Return a new view of the dictionary’s keys

Your nested dictionnary is a completely separate dict, and you can get its own keys with its own keys() method :

entry[2]['N'].keys()

If you want to recursively get all the keys inside nested dictionnaries, you will have to implement a method for that :

entry = {0: {"Q": 0},
         1: {"W": 2, "E": 3, "N": 5, "S": 4, "Q": 0},
         2: {"N": { "Q":{"E"}}},
}


def rec_keys(dictio):
    keys = []
    for (key,value) in dictio.items():
        if isinstance(value, dict):
            keys.extend(rec_keys(value))
        else:
            keys.append(key)
    return keys

print(rec_keys(entry))
# ['Q', 'Q', 'W', 'N', 'S', 'E', 'Q']



回答2:


When you run print(entry[2].keys())

you're asking python "What keys exist in the data corresponding to key '2'? (which in your case is another dictionary)" The answer to which is just 'N'. This is because

entry[2]

is

{"N": { "Q":{"E"}}

which has the single key 'N' and the data '{"Q":{"E"}}'




回答3:


dict.keys only returns the top level keys of the dictionary. If you want to get all nested keys of a dictionary, you will need to define your own function.

# interface for dictionary-like objects
from collections.abc import Mapping

def nested_keys(d) -> set:
    """
    Return a set containing all nested keys.
    """
    # If it is not a dict-like object, return an empty set
    if not isinstance(d, Mapping):
        return set()

    keys = d.keys()
    for v in d.values():
        # Update the keys set with the keys in each value by using the union (or) operator: |
        keys |= nested_keys(v)

    return keys



回答4:


if you wanted to check all nested keys, you could create a loop function that checks the type of your looked up data and iterates over it if it is another dictionary, like

def print_nested_keys(d):
    for k in d.keys():
        print(k)
        if type(d[k]) == dict:
            print('nested dict detected - recursing...')
            print_nested_keys(d[k])

here, whenever one of the keys in your dictionary points to another dictionary, you call the function recursively to read through the lower-level dictionary keys. of course, you could always append the keys that you find in a list to return by your function in the end, if you want such a list.



来源:https://stackoverflow.com/questions/51753809/accessing-nested-keys-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!