简体   繁体   English

如何遍历字典列表并选择特定的键值python

[英]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.

相关问题 如何循环浏览字典列表并提取具有布尔值的键 - How to loop through dictionary list and extract one key with boolean value 如何从python中的字典中选择一个特定的值 - How to select a specific value from a dictionary in python 循环遍历 python 中的嵌套字典并显示键值对 - loop through nested dictionary in python and display key value pair 如何遍历字典列表,并打印出 Ansible 中的每个键和值对 - How to loop through a list of dictionary, and print out every key and value pair in Ansible 在Python中通过字典或列表进行FOR循环时,如何获取项的键? - How do I get the key of an item when doing a FOR loop through a dictionary or list in Python? 遍历python字典中的键值对以构建对象列表 - Iterating through key value pairs in a python dictionary to build an list of objects Python json 按特定键值在列表中查找字典 - Python json find dictionary in list by specific key value 如何在不使用任何for循环的情况下返回字典的特定键值? - How to return a specific key value of dictionary without using any for loop? 如何从 python List-Dictionary 中搜索和删除特定的键:值条目 - How to search and remove specific key:value entries from a python List-Dictionary 如何从python中的嵌套字典中提取特定键的值并返回值列表 - How to extract a specific key's value from a nested dictionary in python and return a list of the values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM