真鸡儿痛苦,网上搜寻了一大堆,都没我想要的,要不是纯字典,要不只能拿到value值,我想要的是具体路径啊!

没把法,只能自己强上了!本来3个函数就能搞定了,根据要找的value值得到该值的具体路径,但是后面又想到有时是要根据key值得到对应的value,而且在镶嵌字典里,一样名字的key应该不少啊,最好还能返回要找的key的具体路径跟它对应的值,具体看代码

之前2个版本都是先找到目标,然后再一步步补全路径,但是这样做太蛋疼,效率太低了!
后来我想通了,应该在初始时遍历每个字典、列表就记录补全路径path才是王道,最后找到了目标,就把path添加到集合list!
这次完美多了!!

class find_path():def __init__(self,target):self.target=targetdef find_the_value(self,target,value,path='',path_list=None):'''完全匹配,每经过一层(list、dict)都会记录path,到了最后一层且当前target就是要找的目标,才把对应的path记录下来:param target: 被搜索的目标:param value: 要搜索的关键字:param path: 当前所在的路径:param path_list: 存放所有path的列表判断当前target类型:···是字典,循环内容,每个键值都记录下路径path,然后以当前值v为判断target,调用自身传入添加了的path判断···是列表,循环内容,每个元素都记录下路径path,然后以当前元素为判断target,调用自身传入添加了的path判断···是str或者int,那么就判断当前target是否就是要搜索的value,如果是,那就把路径path放进list里面'''if isinstance(target, dict):for k, v in target.items():path1 = copy.deepcopy(path)path1=path1+str([k])self.find_the_value(v, value, path1, path_list)elif isinstance(target, (list, tuple)):  # 判断了它是列表for i in target:path1 = copy.deepcopy(path)posi = target.index(i)path1 = path1+'[%s]' % posiself.find_the_value(i, value, path1, path_list)elif isinstance(target, (str, int)) :if  str(value) ==str(target):   #必须完全相同path_list.append(path)def find_in_value(self,target,value,path='',path_list=None):'''包含匹配,内容跟上面一样,只是最后判断时不同'''if isinstance(target, dict):for k, v in target.items():path1 = copy.deepcopy(path)path1=path1+str([k])self.find_in_value(v, value, path1, path_list)elif isinstance(target, (list, tuple)):  # 判断了它是列表for i in target:path1 = copy.deepcopy(path)posi = target.index(i)path1 = path1+'[%s]' % posiself.find_in_value(i, value, path1, path_list)elif isinstance(target, (str, int)) :if  str(value) in str(target):   #path_list.append(path)def find_the_key(self,target,key,path='',path_list=None):'''查找key,每经过一层(list、dict)都会记录path,在字典时,若当前的k就是要找的key,那就把对应的path记录下来:param target: 被搜索的目标:param key: 要搜的键:param path: 当前所在的路径:param path_list: 存放所有path的列表判断当前target类型:···是字典,循环内容,每个键值都记录下路径path,判断当前k是否要查找的:~~~是,那就把路径path放进list里面~~~不是,以当前值v为判断target,调用自身传入添加了的path判断···是列表,循环内容,每个元素都记录下路径path,然后以当前元素为判断target,调用自身传入添加了的path判断'''if isinstance(target, dict):for k, v in target.items():path1 = copy.deepcopy(path)path1=path1+str([k])if str(key) == str(k):path_list.append(path1)else:self.find_the_key(v, key, path1, path_list)elif isinstance(target, (list, tuple)):  # 判断了它是列表for i in target:path1 = copy.deepcopy(path)posi = target.index(i)path1 = path1+'[%s]' % posiself.find_the_key(i, key, path1, path_list)#====================================================================================def in_value_path(self,value):'''包含匹配value'''path_list=[]self.find_in_value(self.target, value,path_list=path_list)return path_listdef the_value_path(self,value):'''完全匹配value'''path_list=[]self.find_the_value(self.target, value,path_list=path_list)return path_listdef the_key_path(self,value):'''只查找key'''path_list = []self.find_the_key( self.target, value,path_list=path_list)return path_list

效果如下:
目标字典:


dict1 = {'a': 5, 'recommend_summoner_skill_tips': '闪现:向指定方向位移一段距离', 'text_price': '', 'be_restrained_hero': [{'hero_id': '3', 'name': '赵云', 'icon': 'http://pictest.wankacn.com/2017-04-26_59005ecc2eda6.png'},{'hero_id': '24', 'name': '宫本武藏', 'icon': 'http://pictest.wankacn.com/2017-04-26_59005ee432c52.png'},{'hero_id': '39', 'name': '韩信', 'icon': 'http://pictest.wankacn.com/2017-04-26_59005eff2e35f.png'}],'rec_inscriptions': [{'title': 'four', 'list': [{'name': '阳炎', 'icon': 'http://pictest.wankacn.com/2017-04-27_5901c32aec40d.png','attrs': '法术攻击+2.5|法术穿透+1.4', 'level': '4'},{'text':10},{'name': '渴血', 'icon': 'http://pictest.wankacn.com/2017-04-27_5901c33051946.png','attrs': '法术攻击+1.4|法术吸血+0.8%|法术防御+1.6', 'level': '4'},{'name': '侵蚀', 'icon': 'http://pictest.wankacn.com/2017-04-27_5901c3367da4c.png','attrs': '法术攻击+0.9|法术穿透+3.8', 'level': '4'}]}, {'title': 'five', 'list': [{'name': '圣人', 'icon': 'http://pictest.wankacn.com/2017-04-27_5901c32b7c6eb.png', 'attrs': '法术攻击+5.3','level': '5'}, {'name': '轮回', 'icon': 'http://pictest.wankacn.com/2017-04-27_5901c3325134d.png','attrs': '法术攻击+2.4|法术吸血+1%', 'level': '5'},{'name': '怜悯', 'icon': 'http://pictest.wankacn.com/2017-04-27_5901c3384f95c.png', 'attrs': '冷却缩减-1%','level': '5'}]}], 'skill_list': [{'attrs': [], 'intro': '被动','description': '被动:王昭君脱离战斗后会获得可抵免450<span style="color:#6edbc8">(+52%法术加成)</span>点伤害的寒冰护盾,护盾破裂时对附近敌人造成一次冰霜冲击,造成450<span style="color:#6edbc8">(+52%法术加成)</span>点<span style="color:#7575d9">法术伤害</span>与减速效果;寒冰护盾有3秒的回复冷却时间','mana_cost': '', 'cd': '', 'tags': '法术', 'name': '冰封之心','icon': 'http://pic.wankacn.com/2017-08-28_59a37c3ac17e6.png'},{'attrs': ['基础伤害|400|480|560|640|720|800'], 'intro': '主升','description': '王昭君操控碎裂冰晶绽开,对范围内的敌军造成400<span style="color:#6edbc8">(+65%法术加成)</span>点<span style="color:#7575d9">法术伤害</span>与减速,并获得他们的视野持续2秒','mana_cost': '80', 'cd': '5', 'tags': '法术/控制', 'name': '凋零冰晶','icon': 'http://pic.wankacn.com/2017-08-28_59a37c3de40b4.png'}, {'attrs': ['基础伤害|250|280|310|340|370|400','冷却时间|10|9.2|8.4|7.6|6.8|6'], 'intro': '副升','description': '王昭君引领寒霜之力,一定时间后将范围内敌人冰冻并造成250<span style="color:#6edbc8">(+47%法术加成)</span>点<span style="color:#7575d9">法术伤害</span>;被动:对被冰冻的敌人造成额外250<span style="color:#6edbc8">(+50%法术加成)</span>点<span style="color:#7575d9">法术伤害</span>','mana_cost': '80', 'cd': '8', 'tags': '法术/控制', 'name': '禁锢寒霜','icon': 'http://pic.wankacn.com/2017-08-28_59a37c4140712.png'},{'attrs': ['基础伤害|300|375|450', '冷却时间|50|45|40'], 'intro': '有大点大','description': '王昭君召唤寒冬之力,在指定位置降下暴风雪对范围内敌人每次打击造成300<span style="color:#6edbc8">(+50%法术加成)</span>点<span style="color:#7575d9">法术伤害</span>与减速,期间获得600护甲加成','mana_cost': '150', 'cd': '50', 'tags': '法术/控制', 'name': '凛冬已至','icon': 'http://pic.wankacn.com/2017-08-28_59a37c4495707.png','text':'what'}],'skill_tips': '使用凋零冰晶使对方减速后再其移动方向的前方一小段距离使用禁锢寒霜。接着立刻贴近使用凛冬已至,配合被动对敌方打出成吨伤害', 'hero_id': '40','half_img': 'http://pic.wankacn.com/2017-08-28_59a3840dd8625.png', 'recommend_summoner_skill': [{'name': '闪现', 'icon': 'http://pictest.wankacn.com/2017-04-27_5901deac66009.jpeg','description': '120秒CD:向指定方向位移一段距离'}], 'ticket_price': '588', 'type': ['2'],'big_img': 'http://pic.wankacn.com/2017-08-28_59a38313260bb.png', 'restrained_hero': [{'hero_id': '10', 'name': '刘禅', 'icon': 'http://pictest.wankacn.com/2017-04-26_59005ed4dfde6.png'},{'hero_id': '23', 'name': '典韦', 'icon': 'http://pictest.wankacn.com/2017-04-26_59005ee344db8.png'},{'hero_id': '48', 'name': '亚瑟', 'icon': 'http://pictest.wankacn.com/2017-04-26_59005f0e9459e.png'}],'gold_price': '8888', 'levels': {'attack': '3', 'skill': '10', 'survival': '2', 'difficulty': '6'},'name': '王昭君','hero_tips': '灵巧走位。尽量不要离敌方太近和太远,太近容易被贴身打击。太远冰冻住以后还没过去对方就解冻了。最好是等待别人与我方火并不能抽身离开时使用禁锢寒霜,给对方一个意想不到的绝望','equip_choice': [{'title': 'KPL职业出装', 'description': '这是KPL职业联赛中AG超玩会.老帅的出装。暴力输出,需要注意入场时机。', 'list': [{'icon': 'http://pictest.wankacn.com/2017-04-28_5903166adc5c9.jpeg', 'equip_id': '40'},{'icon': 'http://pictest.wankacn.com/2017-04-28_59031675e2f15.jpeg', 'equip_id': '75'},{'icon': 'http://pictest.wankacn.com/2017-04-28_5903166b10ebe.jpeg', 'equip_id': '41'},{'icon': 'http://pictest.wankacn.com/2017-04-28_5903166b83498.jpeg', 'equip_id': '43'},{'icon': 'http://pictest.wankacn.com/2017-04-28_5903166c6d0eb.jpeg', 'equip_id': '48'},{'icon': 'http://pictest.wankacn.com/2017-04-28_5903166c96be0.jpeg', 'equip_id': '49','text2':'what'}]},{'title': 'KPL职业出装', 'description': '这是KPL职业联赛中QGhappy.Cat的出装。团队收益装备,梦魇之牙克制地方回血英雄。','list': [{'icon': 'http://pictest.wankacn.com/2017-04-28_5903166ab272c.jpeg','equip_id': '39'},{'icon': 'http://pictest.wankacn.com/2017-04-28_59031676397b3.jpeg','equip_id': '76'},{'icon': 'http://pictest.wankacn.com/2017-04-28_5903166adc5c9.jpeg','equip_id': '40'},{'icon': 'http://pictest.wankacn.com/2017-04-28_5903166b10ebe.jpeg','equip_id': '41'},{'icon': 'http://pictest.wankacn.com/2017-04-28_5903166b5c100.jpeg','equip_id': '42'},{'icon': 'http://pictest.wankacn.com/2017-04-28_5903166c96be0.jpeg','equip_id': '49'}]}, {'title': '强力消耗装','description': '这套装备靠减CD及回蓝,消耗对面英雄。冰霜法杖便宜、增加血量,可以让王昭君更稳健地对线,被动减速配合王昭君2技能很容易创造杀敌机会。','list': [{'icon': 'http://pictest.wankacn.com/2017-04-28_5903166c42ac0.jpeg','equip_id': '47'}, {'icon': 'http://pictest.wankacn.com/2017-04-28_59031676397b3.jpeg','equip_id': '76'}, {'icon': 'http://pictest.wankacn.com/2017-04-28_5903166bda9f0.jpeg','equip_id': '45'}, {'icon': 'http://pictest.wankacn.com/2017-04-28_5903166b10ebe.jpeg','equip_id': '41'}, {'icon': 'http://pictest.wankacn.com/2017-04-28_5903166bb25f7.jpeg','equip_id': '44'}, {'icon': 'http://pictest.wankacn.com/2017-04-28_5903166c96be0.jpeg','equip_id': '49'}]}], 'waht':10,'partner_hero': [{'hero_id': '2', 'name': '小乔', 'icon': 'http://pictest.wankacn.com/2017-04-26_59005ecad0a65.png'},{'hero_id': '5', 'name': '妲己', 'icon': 'http://pictest.wankacn.com/2017-04-26_59005ecec7ee2.png'},{'hero_id': '30', 'name': '武则天', 'icon': 'http://pictest.wankacn.com/2017-04-26_59005eed6c76f.png'}],'melee_tips': '在团战开始前期的迂回还没开始火并的时候跟在团后面用凋零冰晶对敌方进行骚扰和消耗,待开团开始后往人堆里放禁锢寒霜。总能冻住两个以上。接着使用凛冬已至再闪现过去。给对方一个沉重的打击。禁锢寒霜尽量瞄准输出类英雄那样才能凸显凛冬已至的爆炸伤害的效果','history_intro': '昭君,西汉元帝时宫人。《汉书·元帝纪》记载:竟宁元年春,匈奴乎韩邪单于来朝,元帝赐单于待诏掖庭王樯为阏氏。《后汉书》又记王昭君,字嫱。她和亲匈奴后,号“宁胡阏氏”。','background_story': '北方草原上,生活着凶悍的北夷部族,时常冲击长城进行掠夺。陷入内乱的诸侯们不得不送去粮食,布匹与和亲的公主换来停战协议。 \r\n北夷人将公主送往圣地——凛冬之海,献祭给神明作为新娘。久而久之,这演变成一项残忍神圣的传统。 \r\n然而数百年后,统治者们认为不再需要盟约,开始谋划残酷的阴谋:他们乘人们举行祭典毫无防备之际血洗草原,还想夺取蛮夷们献给神明的宝藏。 \r\n刽子手们如愿以偿。他们发出欢呼,忙碌着将成堆黄金带回中原。暴风雪突如其来,随之是浩大的雪崩。 \r\n是北夷人神明的愤怒吗?刚刚还不可一世的士兵哀嚎着逃窜。然而归去的路已被冰雪封锁。他们像琥珀中的小虫般挣扎,眼睁睁看着自己的身躯被冰封进透明的棺材。 \r\n幸存的北夷人从藏身之处走出来,簇拥着他们的公主——王昭君。她高雅,美丽,明亮的双眸饱含哀伤,纤长的手指拂过故乡来客们冰冷狰狞的脸庞。\r\n“告诉我,故乡的梅花开了吗?”','skin_imgs': [{'skin_name': '冰雪之华', 'big_img': 'http://pic.wankacn.com/2017-08-29_59a4cf96438b3.jpeg'},{'skin_name': '精灵公主', 'big_img': 'http://pic.wankacn.com/2017-08-29_59a4cfaca937b.jpeg'},{'skin_name': '偶像歌手', 'big_img': 'http://pic.wankacn.com/2017-08-29_59a4cfc62223c.jpeg'},{'skin_name': '凤凰于飞', 'big_img': 'http://pic.wankacn.com/2017-08-29_59a4cfd3af958.jpeg'},{'skin_name': '幻想奇妙夜', 'big_img': 'http://pic.wankacn.com/2018-03-12_5aa63988e13e9.jpeg'}],'title': '冰雪之华', 'diamond_price': '0'}

运行函数:


a=find_path(dict1)
in_value_path=a.in_value_path('基础伤害') #包含匹配,只要dict/list的元素中包含这个str,就能得到对应的path
the_value_path=a.the_value_path('40')   #完全匹配,只要dict/list的元素中就是这个 str,就能得到对应的path
the_key_path=a.the_key_path('description')#只搜索dict的key,相同的就会得到对应的path
=================================================================================

效果如下:

for i in in_value_path:print(i)print(eval('dict1'+i))

==========================================================================

for i in the_value_path:print(i)print(eval('dict1'+i))

======================================================================


for i in the_key_path:print(i)print(eval('dict1'+i))

python中无限遍历在多维嵌套字典、列表的JSON中获取路径path跟对应的值——find_path(终极版)相关推荐

  1. python中列表嵌套字典/列表排序,字典排序,列表排序。

    列表排序 列表嵌套字典/列表排序 使用 lambda # 列表内多层嵌套,字典和列表 list_num = [{'name': '中国', 'uid': 10003, 'extinfo': {'tie ...

  2. python中字典列表元组_Python中的列表和元组

    python中字典列表元组 Lists and tuples are arguably Python's most versatile, useful data types. You will fin ...

  3. python 字典列表,元组列表 列表嵌套字典 列表嵌套元组 字典嵌套列表

    列表嵌套字典 l=[] for i in alist:kk = {}names.append(i.string)a_url.append(i.get('href'))kk['章节名']=i.strin ...

  4. spark中读取json_【Spark】使用DataFrame读取复杂JSON中的嵌套数组

    众所周知,在早期Spark版本中就已经支持读取Json格式的数据文件,并能够直接转换为数据库表,以方便我们进行处理数据,在本篇文章中我们将介绍如何通过Spark API很简单地读取Json数据,并进一 ...

  5. 3、JavaWeb中Service层的作用、MyBatis的重要组件、mybatis-config.xml中的别名映射、properties配置、#{}和${}的区别、获取插入数据的主键值

    文章目录 1.Service层的作用 2.MyBatis重要组件 Resources SqlSessionFactoryBuilder SqlSessionFactory SqlSession 针对上 ...

  6. 无限遍历,Python实现在多维嵌套字典、列表、元组的JSON中获取数据

    背景 在做接口自动化的过程中,接口返回的数据是  列表字典循环嵌套  格式的,所以怎样通过一个key值,获取到被包裹了多层的目标数据成为了摆在我面前的一个问题. 一开始没想自己写,但是搜索后发现虽然很 ...

  7. python创建与遍历List二维列表

    python 创建List二维列表 lists = [[] for i in range(3)] # 创建的是多行三列的二维列表 for i in range(3):lists[0].append(i ...

  8. python中max()、min()获得最大值与最小值_(Python基础教程之十)Python max()和min()–在列表或数组中查找最大值和最小值...

    使用**max()和min()**方法在可比较元素的集合(例如列表,集合或数组)中查找最大(或最小)项的Python示例. 1. Python max() function max() 该功能用于– ...

  9. python创建列表_python创建与遍历List二维列表的方法

    python 创建List二维列表 lists = [[] for i in range(3)] # 创建的是多行三列的二维列表 for i in range(3): lists[0].append( ...

最新文章

  1. GPT-2的大规模部署:AI Dungeon 2 如何支撑百万级用户
  2. JS判断数字字母中文
  3. 与IO相关的等待事件troubleshooting-系列9
  4. Redis集群监控及Redis桌面客户端
  5. nyoj--32--组合数
  6. 秘籍和修改器之人偶遗迹 ひとがたルイン
  7. 一文看懂Java内存模型(JMM)
  8. 进销存excel_干货分享,上班族必看。EXCEL轻松搞定制作进销存报表
  9. 高德地图热力图,高德自带热力图heatmap
  10. 淘宝网打开很慢、特别慢的完美解决方法
  11. 基于 word2vec 模型的文本分类任务
  12. E45: 'readonly' option is set (add ! to override)
  13. Java开发报表——Grid++Report 报表设计器
  14. 国税计算机专业面试题,2018年国家公务员考试:国税系统面试题
  15. 干货!老司机带你了解网易视频云互动直播,你想看的全都有
  16. I2C协议研读(十)
  17. linux 25端口漏洞,Linux通过栈溢出进行提权实战(dpwwn03)
  18. 病毒检测(BF实现)
  19. 一个实验了解多层内网渗透
  20. 虚拟时钟(Virtual clock)

热门文章

  1. 利用API函数修改PB自带toolbar的字体
  2. 小米电视注册显示访问服务器失败,为什么手机看电视连接上却出现服务器连接异常 – 手机爱问...
  3. 力扣638. 大礼包(回溯法+剪枝优化)
  4. Android 5.0,6.0,7.0,8.0新特性整理
  5. 三星android耗电量大怎么办,三星S9耗电太快怎么办?四个省电小技巧教给你!
  6. java中Stream流的使用
  7. ftp上传文件报错“200 Type set tol.200 PORT command scesful.4MUM np for he Uicde hater eit inte onetmult”
  8. 浅谈selenium4新增功能之相对定位
  9. 怎样避免网站存在逻辑漏洞
  10. AWVS14 Docker Update