Print complete key path for all the values of a python nested dictionary
如果下面是我的嵌套字典,我希望递归地解析并打印所有值以及嵌套键的完整路径。
1 | my_dict = {'attr':{'types':{'tag':{'name':'Tom', 'gender':'male'},'category':'employee'}}} |
预期输出:
1 2 3 4 5 6 | Key structure : my_dict["attr"]["types"]["tag"]["name"] value :"Tom" Key structure : my_dict["attr"]["types"]["tag"]["gender"] value :"male" Key structure : my_dict["attr"]["types"]["category"] value :"employee" |
我写了一个递归函数,但运行到这里:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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=>汤姆
在上面:对于男性,键结构不应包含"类别"如何保持正确的密钥结构?
您不应该更改
1 2 3 4 5 6 7 | 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所提到的,您的问题是在
这里有一个使用递归生成器的替代解决方案,而不是在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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 |
输出
1 2 3 | attr_types_category => employee attr_types_tag_gender => male attr_types_tag_name => Tom |
只是在上面添加@catavaran代码。如果
1 2 3 4 5 6 7 8 9 | 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 |
谢谢你的代码帮助了我。