[英]How to loop through a list of dictionary and select specific key value python
So I have the following list of dictionary: 因此,我有以下词典列表:
"Items": [{
"Instance": 1,
"SubInstance": 1,
"Path": "To Sleep",
},
{
"Instance": 1,
"SubInstance": 2,
"Path": "To Eat",
},
{
"Instance": 3,
"SubInstance": 1,
"Path": "To Play"
},
{
"Instance": 2,
"SubInstance": 1,
"Path": "To Work"
}]
And I am trying to loop through the list and check for if Instance = 3 and SubInstance = 1 then select just the key value "To Play" and assign it to a variable path_result. 我试图遍历该列表,并检查Instance = 3和SubInstance = 1,然后仅选择键值“ To Play”并将其分配给变量path_result。 User input variables to use are var_inst for Instance and var_subInst for Subinstance.
要使用的用户输入变量是Instance的var_inst和Subinstance的var_subInst。 Thanks.
谢谢。
for item in l:
if item['Instance'] == 3 and item['SubInstance'] == 1:
path_result = item['Path']
l is a list of dictionaries l是词典列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.