find the path of the targeted values in a nested python dictionary and list(在嵌套的 python 字典和列表中找到目标值的路径)
问题描述
我遇到了在嵌套的 Python 字典和列表中查找目标值的路径的问题.例如,我有以下 dict,我的目标值是blah blah blah".
I have an issue of finding the path of the targeted values in a nested python dictionary and list. for example, I have following dict, and my targeted value is "blah blah blah".
{ "id" : "abcde",
  "key1" : "blah",
  "key2" : "blah blah",
  "nestedlist" : [ 
    { "id" : "qwerty",
      "nestednestedlist" : [ 
        { "id" : "xyz",
          "keyA" : "blah blah blah" },
        { "id" : "fghi",
          "keyZ" : "blah blah blah" }],
      "anothernestednestedlist" : [ 
        { "id" : "asdf",
          "keyQ" : "blah blah" },
        { "id" : "yuiop",
          "keyW" : "blah" }] } ] } 
我想得到的是这个值在嵌套字典和列表中的路径."nestedlist" - "nestednestedlist" - "keyA"
What I want to get is the path of this value in the nested dictionary and list. "nestedlist" - "nestednestedlist" - "keyA"
我从 在嵌套的 Python 字典和列表中查找所有出现的键并进行了一些更改:
I found this code from Find all occurrences of a key in nested python dictionaries and lists and made some changes:
def find(key,dic_name):
    if isinstance(dic_name, dict):
        for k,v in dic_name.items():          
            if k == 'name' and v == key:
                yield v
            elif isinstance(v,dict):
                for result in find(key,v):
                    yield result
            elif isinstance(v,list):
                for d in v:
                    for result in find(key,d):
                        yield result
但它只能在结果中获取目标值而不能获取路径.任何人都可以帮忙吗?非常感谢
But it can only get the targeted value in the result but not the path. Can anyone help? Thanks a lot
推荐答案
对链接到的代码稍作更改即可产生结果:
a minor change of the code you link to yields the results:
def fun(dct, value, path=()):
    for key, val in dct.items():
        if val == value:
            yield path + (key, )
    for key, lst in dct.items():
        if isinstance(lst, list):
            for item in lst:
                for pth in fun(item, value, path + (key, )):
                    yield pth
对于您的输入:
for item in fun(dct, value='blah blah blah'):
    print(item)
# ('nestedlist', 'nestednestedlist', 'keyA')
# ('nestedlist', 'nestednestedlist', 'keyZ')
<小时>
在您的评论后更新:代码的小改动可以做您想要的:
update after your comment: a minor change of the code can do what you want:
def fun(dct, value, path=()):
    for key, val in dct.items():
        if val == value:
            yield path + (val, )
    for key, lst in dct.items():
        if isinstance(lst, list):
            for item in lst:
                for pth in fun(item, value, path + (dct['id'], key, )):
                    yield pth
示例:
for item in fun(dct, value='xyz'):
    print(item)
# ('abcde', 'nestedlist', 'qwerty', 'nestednestedlist', 'xyz')
这篇关于在嵌套的 python 字典和列表中找到目标值的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在嵌套的 python 字典和列表中找到目标值的路径
 
				
         
 
            
        基础教程推荐
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 包装空间模型 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
				 
				 
				 
				