假设我在列表中有这本字典:
[
{
"Name": "Person",
"Confidence": 97.56156921386719,
"Instances": [
{
"BoundingBox": {
"Width": 0.6137702465057373,
"Height": 0.9175498485565186,
"Left": 0.22297996282577515,
"Top": 0.0739903450012207
},
"Confidence": 94.51961517333984
},
{
"BoundingBox": {
"Width": 0.46570318937301636,
"Height": 0.6405649781227112,
"Left": 0.11447866261005402,
"Top": 0.34079012274742126
},
"Confidence": 82.2153549194336
}
],
"Parents": []
},
{
"Name": "Human",
"Confidence": 97.56156921386719,
"Instances": [],
"Parents": []
},
{
"Name": "Clothing",
"Confidence": 91.08417510986328,
"Instances": [],
"Parents": []
}
}]
如何检索仅具有键值“Name”:“Clothing”的字典?可以从字典中检索到吗?键“服装”可能出现在列表的任何部分。
{
"Name": "Clothing",
"Confidence": 91.08417510986328,
"Instances": [],
"Parents": []
}
最佳答案
您可以使用以下代码获取 Name
为 Clothing
的每个项目:
names = [
{
"Name": "Person",
"Confidence": 97.56156921386719,
"Instances": [
{
"BoundingBox": {
"Width": 0.6137702465057373,
"Height": 0.9175498485565186,
"Left": 0.22297996282577515,
"Top": 0.0739903450012207
},
"Confidence": 94.51961517333984
},
{
"BoundingBox": {
"Width": 0.46570318937301636,
"Height": 0.6405649781227112,
"Left": 0.11447866261005402,
"Top": 0.34079012274742126
},
"Confidence": 82.2153549194336
}
],
"Parents": []
},
{
"Name": "Human",
"Confidence": 97.56156921386719,
"Instances": [],
"Parents": []
},
{
"Name": "Clothing",
"Confidence": 91.08417510986328,
"Instances": [],
"Parents": []
}
]
for name in names:
if name['Name'] == 'Clothing':
print(name)
如果您希望它简短,您可以使用以下代码:
x = [y for y in names if name['Name'] == 'Clothing']
关于python - 获取字典上存在特定键值对的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64565425/