简体   繁体   English

如何从包含键作为字典列表的字典中获取第一个键值对(即“类型”:45)

[英]How do i fetch first key value pair (i.e., 'type': 45) from a dictionary which contains key as list with dictionaries

{'alarms': [{'date': '20170925T235525-0700',
             'id': 8,
             'ip': '172.26.70.4',
             'severity': 4,
             'type': 45},
            {'date': '20170925T235525-0700',
             'id': 7,
             'ip': '172.26.70.4',
             'severity': 4,
             'type': 45},
            {'date': '20170925T235525-0700',
             'id': 6,
             'ip': '172.26.70.4',
             'severity': 4,
             'type': 45},
            {'date': '20170925T220858-0700',
             'id': 5,
             'ip': '172.26.70.4',
             'severity': 6,
             'type': 44},
            {'date': '20170925T220857-0700',
             'id': 4,
             'ip': '172.26.70.4',
             'severity': 6,
             'type': 44},
            {'date': '20170925T220857-0700',
             'id': 3,
             'ip': '172.26.70.4',
             'severity': 6,
             'type': 44},
            {'date': '20170925T220856-0700',
             'id': 2,
             'severity': 6,
             'type': 32},
            {'date': '20170925T220850-0700', 'id': 1, 'severity': 6, 'type': 1},
            {'date': '20170925T220850-0700',
             'id': 0,
             'severity': 6,
             'type': 33}]}

Need to fetch first key value pair (ie, 'type': 45) 需要获取第一个键值对(即“类型”:45)

Kindly guide, I am trying it on Python 2.7. 谨此指导,我正在Python 2.7上进行尝试。

Your data is a dictionary where the `"alarms" key is associated with a list of dictionaries. 您的数据是一个字典,其中“ alarms”键与一个词典列表相关联。

That dictionary is in the list associated with the "alarms" key. 该词典在与"alarms"键关联的列表中。 So you can fetch it with: 因此,您可以通过以下方式获取它:

data['alarms'][0]

with data the variable that stores this structure. data存储该结构的变量。 So: 所以:

>>> data['alarms'][0]
{'date': '20170925T235525-0700', 'severity': 4, 'id': 8, 'ip': '172.26.70.4', 'type': 45}

you will need to do : 您将需要执行以下操作:

def return_correct_dict(data):
    for d in data['alarms']:
        if d.get('type',"") == 45:
            return d

You have a dictionary of a list of dictionaries. 您有词典列表的字典。 Suppose your dictionary is stored in a variable named dict . 假设您的字典存储在名为dict的变量中。

dict['alarm'][0]['type'] will give you the value 45.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用我想要获取的字典的一个键/值对从字典列表中访问字典 - How to access the dictionary from a list of dictionaries using one key/value pair of the dictionary that I want to fetch 如何将新的键值对添加到字典列表中? - How do I add a new key value pair to a list of dictionaries? 如何删除列表中的特定附加字典(即其键和值) - How do you delete a specific appended dictionary (i.e. its key and value) in a list 如何根据字典列字段列表中的键值对过滤 DataFrame 行? - How do I filter DataFrame rows based on a key value pair from a list of dictionaries column field? 如何根据匹配的键:值对组合字典列表中的 N 个字典? - how do I combine N dictionaries in list of dictionaries based on matching key:value pair? 如何将键/值对插入词典列表中包含的每个词典中 - How can I insert a key/value pair into each dictionary contained in a list of dictionaries 如何从字典 python 列表中第一个元素的键中获取值? - How do i get a value from a key in the first element in a list with dictionaries python? 如何检查任何键值对是否在字典列表中多次出现? - How do I check if any key-value pair appears more than once in a list of dictionaries? 如何更改列表中字典中特定键的值类型? - How do I change the value type of a particular key in a dictionary in a list? 如何在条件语句中使用嵌套在列表中的字典键值对的值? - How do I use the value of a key-value pair of a dictionary nested inside a list in a conditional statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM