简体   繁体   English

从Python中的字典列表中提取键值对

[英]extracting key-value pairs from a list of dictionaries in Python

I have dictionary in the following format stored inside a list.Below are listed the 12 members (dictionaries) of list switch_ports 我有字典中存储的一内部list.Below以下格式列出列表的12个成员(字典) switch_ports

[
{'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:011', 'desc': 's1-eth1'},
{'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:012', 'desc': 's1-eth2'},
{'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:021', 'desc': 's2-eth1'},
{'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:022', 'desc': 's2-eth2'},
{'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:031', 'desc': 's3-eth1'},
{'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:032', 'desc': 's3-eth2'},
{'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:041', 'desc': 's4-eth1'},
{'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:042', 'desc': 's4-eth2'},
{'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:051', 'desc': 's5-eth1'},
{'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:052', 'desc': 's5-eth2'},
{'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:061', 'desc': 's6-eth1'},
{'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:062', 'desc': 's6-eth2'}]

From the list switch_ports I want to access port_id above. switch_ports列表switch_ports访问上面的port_id How do I do that? 我怎么做?

If I do 如果我做

for port in switch_ports:
      print(port)

I get the same output as above. 我得到与上述相同的输出。 However If I try to access individual key-value pairs as below. 但是,如果我尝试按以下方式访问各个键值对。

for port in switch_ports:
      print(port[port_id])

How do I get the port_id (eg 00:00:00:00:00:00:02:062 ) from the list switch_ports 如何从列表switch_ports获取port_id (例如00:00:00:00:00:00:02:062

You are almost there. 你快到了。 Just use port_id as a string, like this 只需将port_id用作字符串,像这样

for port in switch_ports:
      print(port["port_id"])

If you want to gather all the port_id s as a list, then use list comprehension like this 如果您想将所有port_id收集为列表,请像这样使用列表理解

port_ids = [port["port_id"] for port in switch_ports]

Online Demo 在线演示

An alternative, use itemgetter from the operator module: 另一种方法是,使用operator模块中的itemgetter

>>> import operator
>>> operator.itemgetter('port_id')
<operator.itemgetter object at 0x7f318d7b8b90>
>>> port_id = operator.itemgetter('port_id')
>>> for port in switch_ports:
...     print port_id(port)
... 

prints: 打印:

00:00:00:00:00:00:02:011
00:00:00:00:00:00:02:012
00:00:00:00:00:00:02:021
00:00:00:00:00:00:02:022
00:00:00:00:00:00:02:031
00:00:00:00:00:00:02:032
00:00:00:00:00:00:02:041
00:00:00:00:00:00:02:042
00:00:00:00:00:00:02:051
00:00:00:00:00:00:02:052
00:00:00:00:00:00:02:061
00:00:00:00:00:00:02:062

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

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