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 中的这个嵌套字典中搜索特定键?


基础教程推荐
- 修改列表中的数据帧不起作用 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 包装空间模型 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01