![](/img/trans.png)
[英]Get dictionaries in a list where the key:value pairs are in another list of dictionaries
[英]Get the dictionary on a list where there is a specific key value pairs
假設我在列表中有這本字典:
[
{
"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
of 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']
您可以使用這個簡單的list comprehension
:
[print(dictionary) for dictionary in lst if 'Name' in dictionary.keys() and dictionary['Name'] == 'Clothing']
輸出:
{'Name': 'Clothing', 'Confidence': 91.08417510986328, 'Instances': [], 'Parents': []}
這是完整的代碼:
lst = [
{
"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": []
}
]
[print(dictionary) for dictionary in lst if 'Name' in dictionary.keys() and dictionary['Name'] == 'Clothing']
.get(Key, defaultValue)
可以在這里使用。
.get("Name", "")
--> 如果鍵Name
在字典中不存在,則返回一個空字符串。 doc .get("Name")
--> 如果Name
不存在,則返回None
。
所以.get("Name") == "Clothing"
是選擇字典的標准。
並使用filter()僅選擇滿足條件的那些。
把這一切放在一起
filter(lambda x: x.get("Name") == "Clothing", mylist)
filter 返回一個迭代器,因此您可以將其傳遞給 list 以創建完整列表。 如果您只想迭代它,則無需調用 list。
所以,
list(filter(lambda x: x.get("Name") == "Clothing", mylist))
假設你的列表被稱為my_list
,那么這應該給你你想要的:
my_list_new = [x for x in my_list if ("Name", "Clothing") in x.items()]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.