python列表里面有多个字典,怎么取出每个字典特定key的value组成列表?

wj13735546642 2020-04-16 07:50:05
python列表里面有多个字典,怎么取出每个字典特定key的value组成列表?
列表如下
[
{'name': 'eth0', 'description': 'Broadcom Inc. and subsidiaries NetXtreme BCM5719 Gigabit Ethernet PCIe', 'status': '1', 'macAddr': '2c:44:fd:7f:56:a4'},
{'name': 'eth5', 'description': 'Emulex Corporation OneConnect 10Gb NIC (be3)', 'status': '0', 'macAddr': 'd8:9d:67:f2:ef:74'},
{'name': 'eth3', 'description': 'Broadcom Inc. and subsidiaries NetXtreme BCM5719 Gigabit Ethernet PCIe', 'status': '1', 'macAddr': '2c:44:fd:7f:56:a7'},
{'name': 'eth1', 'description': 'Broadcom Inc. and subsidiaries NetXtreme BCM5719 Gigabit Ethernet PCIe', 'status': '1', 'macAddr': '2c:44:fd:7f:56:a5'},
{'name': 'eth4', 'description': 'Emulex Corporation OneConnect 10Gb NIC (be3)', 'status': '0', 'macAddr': 'd8:9d:67:f2:ef:70'},
{'name': 'eth2', 'description': 'Broadcom Inc. and subsidiaries NetXtreme BCM5719 Gigabit Ethernet PCIe', 'status': '0', 'macAddr': '2c:44:fd:7f:56:a6'}
]
取出每个列表key为macAddr的对应value,组成列表,例如这种
["2c:44:fd:7f:56:a4","d8:9d:67:f2:ef:74".......]
...全文
7060 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_40966318 2021-03-12
  • 打赏
  • 举报
回复
from operator import itemgetter map(itemgetter('macAddr'), list_variable)
德意志之力 2021-02-03
  • 打赏
  • 举报
回复
参考下jsonpath
蓝色的胖猫 2021-01-07
  • 打赏
  • 举报
回复
##这里我只取了name和status两个字段作为例子。 data = [ {'name': 'eth0', 'description': 'Broadcom Inc. and subsidiaries NetXtreme BCM5719 Gigabit Ethernet PCIe', 'status': '1', 'macAddr': '2c:44:fd:7f:56:a4'}, {'name': 'eth5', 'description': 'Emulex Corporation OneConnect 10Gb NIC (be3)', 'status': '0', 'macAddr': 'd8:9d:67:f2:ef:74'}, {'name': 'eth3', 'description': 'Broadcom Inc. and subsidiaries NetXtreme BCM5719 Gigabit Ethernet PCIe', 'status': '1', 'macAddr': '2c:44:fd:7f:56:a7'}, {'name': 'eth1', 'description': 'Broadcom Inc. and subsidiaries NetXtreme BCM5719 Gigabit Ethernet PCIe', 'status': '1', 'macAddr': '2c:44:fd:7f:56:a5'}, {'name': 'eth4', 'description': 'Emulex Corporation OneConnect 10Gb NIC (be3)', 'status': '0', 'macAddr': 'd8:9d:67:f2:ef:70'}, {'name': 'eth2', 'description': 'Broadcom Inc. and subsidiaries NetXtreme BCM5719 Gigabit Ethernet PCIe', 'status': '0', 'macAddr': '2c:44:fd:7f:56:a6'} ] end_list = [] for item in data: dic_key = {} dic_key['name'] = item['name'] dic_key['status'] = item['status'] end_list.append(dic_key) print(end_list) ####### [{'name': 'eth0', 'status': '1'}, {'name': 'eth5', 'status': '0'}, {'name': 'eth3', 'status': '1'}, {'name': 'eth1', 'status': '1'}, {'name': 'eth4', 'status': '0'}, {'name': 'eth2', 'status': '0'}]
  • 打赏
  • 举报
回复
同问,不知如何解决的
giter888 2020-04-17
  • 打赏
  • 举报
回复 5
list_result = [item['macAddr'] for item in data]

weixin_43712138 2020-04-16
  • 打赏
  • 举报
回复
b = [{'name': 'eth0', 'description': 'Broadcom Inc. and subsidiaries NetXtreme BCM5719 Gigabit Ethernet PCIe', 'status': '1', 'macAddr': '2c:44:fd:7f:56:a4'}, {'name': 'eth5', 'description': 'Emulex Corporation OneConnect 10Gb NIC (be3)', 'status': '0', 'macAddr': 'd8:9d:67:f2:ef:74'}, {'name': 'eth3', 'description': 'Broadcom Inc. and subsidiaries NetXtreme BCM5719 Gigabit Ethernet PCIe', 'status': '1', 'macAddr': '2c:44:fd:7f:56:a7'}, {'name': 'eth1', 'description': 'Broadcom Inc. and subsidiaries NetXtreme BCM5719 Gigabit Ethernet PCIe', 'status': '1', 'macAddr': '2c:44:fd:7f:56:a5'}, {'name': 'eth4', 'description': 'Emulex Corporation OneConnect 10Gb NIC (be3)', 'status': '0', 'macAddr': 'd8:9d:67:f2:ef:70'}, {'name': 'eth2', 'description': 'Broadcom Inc. and subsidiaries NetXtreme BCM5719 Gigabit Ethernet PCIe', 'status': '0', 'macAddr': '2c:44:fd:7f:56:a6'}] c = [] for i in b: a = [v for k,v in i.items() if 'macAddr' == k] c.append(a) print(c)
chuifengde 2020-04-16
  • 打赏
  • 举报
回复 1
l = [ i['macAddr']  for i in 列表 ]
Tyaing 2021-12-14
  • 举报
回复
@chuifengde 学到了,这个方法最简便
zowhl 2022-04-07
  • 举报
回复
@chuifengde 谢谢
weixin_43712138 2020-04-16
  • 打赏
  • 举报
回复
[v for k,v in ***.items if k ==macAddr] #***为字典名称
weixin_43712138 2020-04-16
  • 打赏
  • 举报
回复
[v for k,v in ***.items] #***为字典名字
paullbm 2020-04-16
  • 打赏
  • 举报
回复
paullbm 2020-04-16
  • 打赏
  • 举报
回复 1

all_adapter=[{'name': 'eth0', 'description': 'Broadcom Inc. and subsidiaries NetXtreme BCM5719 Gigabit Ethernet PCIe', 'status': '1', 'macAddr': '2c:44:fd:7f:56:a4'}, \
{'name': 'eth5', 'description': 'Emulex Corporation OneConnect 10Gb NIC (be3)', 'status': '0', 'macAddr': 'd8:9d:67:f2:ef:74'},  \
{'name': 'eth3', 'description': 'Broadcom Inc. and subsidiaries NetXtreme BCM5719 Gigabit Ethernet PCIe', 'status': '1', 'macAddr': '2c:44:fd:7f:56:a7'}, \
{'name': 'eth1', 'description': 'Broadcom Inc. and subsidiaries NetXtreme BCM5719 Gigabit Ethernet PCIe', 'status': '1', 'macAddr': '2c:44:fd:7f:56:a5'}, \
{'name': 'eth4', 'description': 'Emulex Corporation OneConnect 10Gb NIC (be3)', 'status': '0', 'macAddr': 'd8:9d:67:f2:ef:70'}, \
{'name': 'eth2', 'description': 'Broadcom Inc. and subsidiaries NetXtreme BCM5719 Gigabit Ethernet PCIe', 'status': '0', 'macAddr': '2c:44:fd:7f:56:a6'}]

macAddrList=[]
for net_adapter_dict in all_adapter:
    macAddrList.append(net_adapter_dict['macAddr'])
    
print(macAddrList)

37,742

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧