我是靠谱客的博主 执着小蜜蜂,这篇文章主要介绍Python多层字典取值原文地址:https://www.cnblogs.com/Detector/p/8085460.html,现在分享给大家,希望可以做个参考。
#! /usr/bin/python
# coding:utf-8
"""
@author:Bingo.he
@file: get_target_value.py
@time: 2017/12/22
"""
def get_target_value(key, dic, tmp_list):
"""
:param key: 目标key值
:param dic: JSON数据
:param tmp_list: 用于存储获取的数据
:return: list
"""
if not isinstance(dic, dict) or not isinstance(tmp_list, list): # 对传入数据进行格式校验
return 'argv[1] not an dict or argv[-1] not an list '
if key in dic.keys():
tmp_list.append(dic[key]) # 传入数据存在则存入tmp_list
else:
for value in dic.values(): # 传入数据不符合则对其value值进行遍历
if isinstance(value, dict):
get_target_value(key, value, tmp_list) # 传入数据的value值是字典,则直接调用自身
elif isinstance(value, (list, tuple)):
_get_value(key, value, tmp_list) # 传入数据的value值是列表或者元组,则调用_get_value
return tmp_list
def _get_value(key, val, tmp_list):
for val_ in val:
if isinstance(val_, dict):
get_target_value(key, val_, tmp_list) # 传入数据的value值是字典,则调用get_target_value
elif isinstance(val_, (list, tuple)):
_get_value(key, val_, tmp_list) # 传入数据的value值是列表或者元组,则调用自身

不好意思,这篇文章也是抄的
正所谓抄袭一时爽,一直抄一直爽,哈哈哈
讲究一个资源共享,知识共享,知识不是用来卖钱的,是用来传承的
原文地址:https://www.cnblogs.com/Detector/p/8085460.html
最后
以上就是执着小蜜蜂最近收集整理的关于Python多层字典取值原文地址:https://www.cnblogs.com/Detector/p/8085460.html的全部内容,更多相关Python多层字典取值原文地址:https://www内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复