2 回答
TA贡献1784条经验 获得超8个赞
record = [({
"first_name":"nadbor",
"last_name":"drozd",
"occupation":"data scientist",
"markingAgentUsed":[
{
"consumableLabelCode":"L",
"amountUsed":{
"amount" : 100,
"unit": "litre"
}
},
{
"consumableLabelCode":"Y",
"amountUsed":{
"amount" : 300,
"unit": "mililitre"
}
},
{
"consumableLabelCode":"0",
"amountUsed":{
"amount" : 999,
"unit": "gallon"
}
}
]
})]
userKey = '0'
for entry in record[0]['markingAgentUsed']:
if entry['consumableLabelCode'] == userKey:
print("Amount - {}".format(entry['amountUsed']['amount']) )
print("Units - {}".format(entry['amountUsed']['unit']) )
TA贡献1811条经验 获得超5个赞
这一切都是关于理解数据结构的嵌套(这不是一个命名的元组,而是一个字典元组列表和一个字典列表,其中一些键本身就是字典:-/很少!这段代码提取了你的内容要求:
inp = '0'
for ma in record[0]['markingAgentUsed']:
if ma['consumableLabelCode'] == inp:
print(ma['amountUsed']['amount'], ma['amountUsed']['unit'])
添加回答
举报