简体   繁体   English

遍历键和值对的列表,并使用Python获取特定的键和值

[英]Iterate through a list of key and value pairs and get specific key and value using Python

I have a list like this. 我有一个这样的清单。

data = [{
'category': 'software', 
'code': 110, 
'actual': '["5.1.4"]', 
'opened': '2018-10-16T09:18:12Z', 
'component_type': 'update', 
'event': 'new update available', 
'current_severity': 'info', 
'details': '', 
'expected': None, 
'id': 10088862, 
'component_name': 'Purity//FA'
}, 
{
'category': 'software', 
'code': 67, 
'actual': None, 
'opened': '2018-10-18T01:14:45Z', 
'component_type': 'host', 
'event': 'misconfiguration', 
'current_severity': 'critical', 
'details': '', 
'expected': None, 
'id': 10088898, 
'component_name': 'pudc-vm-001'
}, 
{
'category': 'array', 
'code': 42, 
'actual': None, 
'opened': '2018-11-22T22:27:29Z', 
'component_type': 'hardware', 
'event': 'failure', 
'current_severity': 'warning', 
'details': ''  , 
'expected': None, 
'id': 10089121, 
'component_name': 'ct1.eth15'
}]

I want to iterate over this and get only category , component_type , event and current_severity . 我想对此进行迭代,并且只获取categorycomponent_typeeventcurrent_severity

I tried a for loop but it says too values to unpack, obviously. 我尝试了一个for循环,但是很明显它说值太高。

for k, v, b, n in data:
                print(k, v, b, n) //do something

i essentially want a list that is filtered to have only category , component_type , event and current_severity . 我本质上希望过滤的列表仅具有categorycomponent_typeeventcurrent_severity So that i can use the same for loop to get out my four key value pairs. 这样我就可以使用相同的for循环获取我的四个键值对。

Or if there is a better way to do it? 还是有更好的方法呢? Please help me out. 请帮帮我。

Note: The stanzas in the list is not fixed, it keeps changing, it might have more than three stanzas. 注:列表中的节不是固定的,它会不断变化,可能有三个以上的节。

If you know that every dict inside your current list of dicts should have at least the keys you're trying to extract their data, then you can use dict[key] , however for safety, i prefer using dict.get(key, default value) like this example: 如果您知道当前字典列表中的每个字典都至少应包含要尝试提取其数据的键,则可以使用dict[key] ,但是为了安全起见,我更喜欢使用dict.get(key, default value)例如以下示例:

out = [
    {
        'category': elm.get('category'),
        'component_type': elm.get('component_type'),
        'event': elm.get('event'),
        'current_severity': elm.get('current_severity')
    } for elm in data
]
print(out)

Output: 输出:

[{'category': 'software',
  'component_type': 'update',
  'current_severity': 'info',
  'event': 'new update available'},
 {'category': 'software',
  'component_type': 'host',
  'current_severity': 'critical',
  'event': 'misconfiguration'},
 {'category': 'array',
  'component_type': 'hardware',
  'current_severity': 'warning',
  'event': 'failure'}]

For more informations about when we should use dict.get() instead of dict[key] , see this answer 有关何时应使用dict.get()而不是dict[key]更多信息,请参见此答案。

with this you get a new list with only the keys you are interested on: 这样,您将获得一个仅包含您感兴趣的键的新列表:

new_list = [{
    'category': stanza['category'],
    'component_type': stanza['component_type'],
    'event': stanza['event'],
 } for stanza in data]

You have a list of dictionaries, simple way to iterate over this is 您有字典列表,对此进行迭代的简单方法是

category = [x['category'] for x in data]

Which prints the values of category key 哪个打印类别键的值

['software', 'software', 'array']

Do the same for component_type , event and current_severity and you're good to go component_typeeventcurrent_severity做同样的event ,你很高兴

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM