How can I search for specific keys in this nested dictionary in Python?(如何在 Python 中的这个嵌套字典中搜索特定键?)
问题描述
我是编程新手,我正在尝试从亚马逊 MWS API 中提取值.它返回我见过的最大的嵌套字典,但我无法弄清楚如何遍历它以找到我需要的值.
I am new to programming and I'm trying to extract values from the Amazon MWS API. It returns the biggest nested dictionary I've seen and I'm having trouble figuring out how to loop through it to find the value I need.
示例:
{'Products': {'Product': {'AttributeSets': {'ItemAttributes': {'Binding': {'value': 'Video '
'Game'},
'Brand': {'value': 'Nintendo'},
'Color': {'value': 'blue'},
'ESRBAgeRating': {'value': 'Everyone'},
'HardwarePlatform': {'value': 'Android/iOS'},
'IsAdultProduct': {'value': 'false'},
'ItemDimensions': {'Height': {'Units': {'value': 'inches'},
'value': '5.00'},
'Length': {'Units': {'value': 'inches'},
'value': '5.00'},
'Weight': {'Units': {'value': 'pounds'},
'value': '0.10'},
'Width': {'Units': {'value': 'inches'},
'value': '2.00'}},
'Label': {'value': 'Nintendo'},
'Languages': {'Language': {'Name': {'value': 'english'},
'Type': {'value': 'Unknown'}}},
'ListPrice': {'Amount': {'value': '34.99'},
'CurrencyCode': {'value': 'USD'}},
'Manufacturer': {'value': 'Nintendo'},
'Model': {'value': 'PMCAPBAA'},
'OperatingSystem': [{'value': 'Not '
'Machine '
'Specific'},
{'value': 'Android/iOS'}],
'PackageDimensions': {'Height': {'Units': {'value': 'inches'},
'value': '1.00'},
'Length': {'Units': {'value': 'inches'},
'value': '5.10'},
'Weight': {'Units': {'value': 'pounds'},
'value': '0.04'},
'Width': {'Units': {'value': 'inches'},
'value': '2.90'}},
'PackageQuantity': {'value': '1'},
'PartNumber': {'value': 'PMCAPBAA'},
'Platform': [{'value': 'Not '
'Machine '
'Specific'},
{'value': 'Android'},
{'value': 'iOS'},
{'value': 'Nintendo '
'Wii'}],
'ProductGroup': {'value': 'Video '
'Games'},
'ProductTypeName': {'value': 'VIDEO_GAME_ACCESSORIES'},
'Publisher': {'value': 'Nintendo'},
'ReleaseDate': {'value': '2016-09-16'},
'SmallImage': {'Height': {'Units': {'value': 'pixels'},
'value': '75'},
'URL': {'value': 'http://ecx.images-amazon.com/images/I/41%2B1-PwEz4L._SL75_.jpg'},
'Width': {'Units': {'value': 'pixels'},
'value': '75'}},
'Studio': {'value': 'Nintendo'},
'Title': {'value': 'Nintendo '
'Pokemon '
'Go '
'Plus'},
'lang': {'value': 'en-US'}}},
'Identifiers': {'MarketplaceASIN': {'ASIN': {'value': 'B01H482N6E'},
'MarketplaceId': {'value': 'ATVPDKIKX0DER'}}},
'Relationships': {},
'SalesRankings': {'SalesRank': [{'ProductCategoryId': {'value': 'video_games_display_on_website'},
'Rank': {'value': '35'}},
{'ProductCategoryId': {'value': '14218821'},
'Rank': {'value': '1'}},
{'ProductCategoryId': {'value': '471304'},
'Rank': {'value': '22'}}]}}}}
我怎样才能找到Title"的值,或者从这本字典中找到Title"值的最佳方法是什么?
How can I find the value of 'Title' or what would be the best way to find the value 'Title' from this dictionary?
当我尝试实现在 Stackoverflow 上找到的类似递归解决方案时,我得到一个 None 值.
When I try to implement similar recursive solutions I found on Stackoverflow, I get a None value.
我试过了:
def recursive_lookup(k, d):
if k in d:
return d[k]
for v in d.values():
if isinstance(v, dict):
return recursive_lookup(k, v)
return None
print(recursive_lookup('Title', productData.parsed))
推荐答案
您的代码的问题在于您没有回溯到其他词典.在返回之前,您不会遍历所有项目.通过返回,您可以切断循环.
The problem with your code is that you are not backtracking to other dictionaries. You don't loop through all the items before you return. By returning you cut off your loop.
下面的代码循环遍历所有字典,直到找到密钥或检查所有字典但什么也没找到才返回.
The code below loops through all of the dicts and doesn't return until it has either found the key or it has checked all the dicts and found nothing.
def recursive_lookup(k, d):
if k in d: return d[k]
for v in d.values():
if isinstance(v, dict):
a = recursive_lookup(k, v)
if a is not None: return a
return None
这篇关于如何在 Python 中的这个嵌套字典中搜索特定键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Python 中的这个嵌套字典中搜索特定键?
基础教程推荐
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01