打印 python 嵌套字典的所有值的完整键路径

Print complete key path for all the values of a python nested dictionary

如果下面是我的嵌套字典,我想递归解析并打印所有值以及嵌套键的完整路径。

my_dict = {'attr':{'types':{'tag':{'name':'Tom', 'gender':'male'},'category':'employee'}}}

预期输出:

Key structure : my_dict["attr"]["types"]["tag"]["name"]<br>
value : "Tom"<br>
Key structure : my_dict["attr"]["types"]["tag"]["gender"]<br>
value : "male"<br>
Key structure : my_dict["attr"]["types"]["category"]<br>
value : "employee"<br>

我写了一个递归函数,但是运行这个:

my_dict = {'attr':{'types':{'tag':{'name':'Tom','gender':'male'},'category':'employee'}}}

def dict_path(path,my_dict):
    for k,v in my_dict.iteritems():
        if isinstance(v,dict):
            path=path+"_"+k
            dict_path(path,v)
        else:
            path=path+"_"+k
            print path,"=>",v

    return
dict_path("",my_dict)

输出:

_attr_types_category => 员工
_attr_types_category_tag_gender => 男
_attr_types_category_tag_gender_name => 汤姆

在上面:对于男性,关键结构不应包含 "category" 如何保留正确的密钥结构?

您不应更改 dict_path() 函数中的 path 变量:

def dict_path(path,my_dict):
    for k,v in my_dict.iteritems():
        if isinstance(v,dict):
            dict_path(path+"_"+k,v)
        else:
            print path+"_"+k,"=>",v
dict_path("",my_dict)

正如 catavaran 提到的,您的问题是由于将新路径组件添加到 for 循环内的 path 变量引起的。您需要将新路径放入调用中,以便它传递到下一级递归,并且不会干扰当前递归级别的 for 循环中后续项目的路径。

这是一个使用递归生成器的替代解决方案,而不是在 dict_path 函数中打印结果。 (FWIW,我使用 print json.dumps(my_dict, indent=4) 重新格式化字典)。

my_dict = {
    "attr": {
        "types": {
            "category": "employee", 
            "tag": {
                "gender": "male", 
                "name": "Tom"
            }
        }
    }
}

def dict_path(my_dict, path=None):
    if path is None:
        path = []
    for k,v in my_dict.iteritems():
        newpath = path + [k]
        if isinstance(v, dict):
            for u in dict_path(v, newpath):
                yield u
        else:
            yield newpath, v

for path, v in dict_path(my_dict):
    print '_'.join(path), "=>", v

输出

attr_types_category => employee
attr_types_tag_gender => male
attr_types_tag_name => Tom

只是添加到上面的@catavaran 代码。 如果 dict 值为 list,并且 list 可能有 dict 或值本身,那么此代码可能会有所帮助。 我只是将分隔符修改为点。

def dict_path(path,my_dict):
    for k,v in my_dict.iteritems():
        if isinstance(v,list):
            for i, item in enumerate(v):
                dict_path( path + "." + k + "." + str(i), item)
        elif isinstance(v,dict):
            dict_path(path+"."+k,v)
        else:
            print path+"."+k, "=>", v

谢谢@catavaran,你的代码对我有帮助。