[英]Get key hierarchy from a nested dict of other lists/dicts in Python
[英]Get value from key within nested Lists of Dicts
我是 Python 的新手,一直在努力打印出附加到鍵的值,該鍵存在於 881569990078588 的嵌套字典列表中。這是我所知道的結構:
詳細信息 - 字典列表
scorecardDetails - 字典列表
記分卡 - 字典
playerHandicap - 鍵:值
還值得注意的是,在與“詳細信息”相同的級別上還有另一個名為“摘要”的列表。
這是我目前所處的位置,但我正在努力找出如何確定我只想查看“playerHandicap”鍵:
details_list = json_load['details']
for index in range(len(details_list)):
for key in details_list[index]:
print(details_list[index][key])
這是 JSON 的快照(所需的鍵/值對在“記分卡”字典中,其中有多個):
"details":[
{
"startTime":"2021-03-16T12:16:16.000Z",
"formattedStartTime":"2021-03-16T12:16:16Z",
"scorecardDetails":[
{
"scorecard":{
"id":172482642,
"customerId":"******",
"playerProfileId":*****,
"roundPlayerName":"*******",
"connectDisplayName":"********",
"courseGlobalId":21042,
"courseSnapshotId":43716,
"frontNineGlobalCourseId":21042,
"scoreType":"STROKE_PLAY",
"useHandicapScoring":true,
"useStrokeCounting":false,
"startTime":"2021-03-16T12:16:16.000Z",
"formattedStartTime":"2021-03-16T12:16:16Z",
"endTime":"2021-04-23T09:09:47.000Z",
"formattedEndTime":"2021-04-23T09:09:47Z",
"unitId":"1",
"roundType":"ALL",
"inProgress":false,
"excludeFromStats":false,
"holesCompleted":18,
"publicRound":false,
"score":29,
"playerHandicap":0,
"courseHandicapStr":"061018120208141604010709111517031305",
"teeBox":"null",
"handicapType":"MEN",
"teeBoxRating":73.03,
"teeBoxSlope":118,
"lastModifiedDt":"2021-04-23T09:09:46.000Z",
"sensorOnPutter":false,
"handicappedStrokes":101,
"strokes":101,
我確信這是一個簡單的解決方案,但我很難理解不同級別的循環:謝謝 :)
下面的代碼將幫助你
for item in json_load['details']:
for scorecardDetail in item['scorecardDetails']:
if 'playerHandicap' in scorecardDetail['scorecard']:
print(scorecardDetail['scorecard']['playerHandicap'])
我不確定是否理解您的問題。
您可以使用 dict.keys() 獲取字典中的所有鍵。
這可能是一個回應: https://stackoverflow.com/a/7002449/19074450
dict_test = {"details":[
{
"startTime":"2021-03-16T12:16:16.000Z",
"formattedStartTime":"2021-03-16T12:16:16Z",
"scorecardDetails":[
{
"scorecard":{
"id":172482642,
"customerId":"6300102921355894",
"playerProfileId":62337607,
"roundPlayerName":"*******",
"connectDisplayName":"5536cd4a-f35e-4034-966c-a5d8238a8c26",
"courseGlobalId":21042,
"courseSnapshotId":43716,
"frontNineGlobalCourseId":21042,
"scoreType":"STROKE_PLAY",
"useHandicapScoring":True,
"useStrokeCounting":False,
"startTime":"2021-03-16T12:16:16.000Z",
"formattedStartTime":"2021-03-16T12:16:16Z",
"endTime":"2021-04-23T09:09:47.000Z",
"formattedEndTime":"2021-04-23T09:09:47Z",
"unitId":"1",
"roundType":"ALL",
"inProgress":False,
"excludeFromStats":False,
"holesCompleted":18,
"publicRound":False,
"score":29,
"playerHandicap":0}}]}]}
details_list = dict_test['details']
for index in details_list:
for dict_ in details_list:
for key_ in dict_.keys():
print(dict_[key_])
優雅的解決方案是使用 jmespath 它還將處理密鑰不存在的情況
pip install jmespath
如果您有data
作為正確的字典,那么我們可以使用
import jmespath
expression = jmespath.compile('details[*].scorecardDetails[*].scorecard.playerHandicap')
# this expression will look in each detail then each scorecardDetails then in each scorecard it will fetch playerHandicap
res = expression.search(data)
print(res) # this will print the list of values from all records
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.