简体   繁体   English

Python 3.6-在列表中搜索包含字典的单词,该列表中包含字典

[英]Python 3.6 - Search word in a list that contain dictionary that containt dictionary in list

I tried to do a simple title but I'm a bit lost with this one. 我试图做一个简单的标题,但是我对这个标题有些迷惑。

I'm using a python module Python-HPILO in order to gather some information like Hard Drive Disk and DIMM details on HP Blades, this is working. 我正在使用python模块Python-HPILO来收集诸如HP Blades上的硬盘信息和DIMM详细信息之类的信息,这是可行的。

Example : 范例:

import hpilo
ilo = hpilo.Ilo('10.0.0.1', 'USER', 'PASSWORD')
health = ilo.get_embedded_health()
logical_drives = (health['storage']['Controller on System Board']['logical_drives'])
print(logical_drives)

Result (anonymized) : 结果(匿名):

[{**'label': '01', 'status': 'OK', 'capacity': '419 GiB', 'fault_tolerance': 'RAID 1/RAID 1+0', 'logical_drive_type': 'Data LUN', 'encryption_status': 'Not Encrypted', 'physical_drives': **[{**'label': 'Port 1I Box 1 Bay 1', 'status': 'OK', 'serial_number': 'SXXXXXXXX', 'model': 'YYYYYY', 'capacity': '419 GiB', **'marketing_capacity': '450 GB'**, 'location': 'Port 1I Box 1 Bay 1', 'fw_version': 'HPD9', 'drive_configuration': 'Configured', 'encryption_status': 'Not Encrypted', 'media_type': 'HDD'**}**, {'label': 'Port 1I Box 1 Bay 2', 'status': 'OK', 'serial_number': 'SXXXXXX', 'model': 'YYYYYY', 'capacity': '419 GiB', 'marketing_capacity': '450 GB', 'location': 'Port 1I Box 1 Bay 2', 'fw_version': 'HPD9', 'drive_configuration': 'Configured', 'encryption_status': 'Not Encrypted', 'media_type': 'HDD'**}]}]

I would like to collect the value of marketing_capacity, which is 450GB here. 我想收集marketing_capacity的价值,这里是450GB。 I found some other topics related to incursive search in list or dict but I were not able to make it work with this specific listed-dictonary-listed-dictonary thing. 我在list或dict中发现了其他一些与递归搜索相关的主题,但是我无法使其与此特定的list-dictonary-listed-dictonary一起使用。

Does anyone have a simple way/function to do research in this kind of list ? 有人在这种列表中有简单的方法/功能来进行研究吗?

You can access that variable by 您可以通过以下方式访问该变量

logical_drives[0]['physical_drives'][0]['marketing_capacity']

'450 GB' 450 GB


If the nesting of your dictionary changes you can unwrap both the lists and dictionaries in your list, and then extract the desired value 如果字典的嵌套发生变化,则可以解开列表中的列表和字典,然后提取所需的值

val = 'marketing_capacity' val ='marketing_capacity'

def unwrap(dic):    
    temp = {}
    for i in dic:
        if type(i) in [list, dict]: temp.update(unwrap(i))
        elif type(dic[i]) in [list, dict]: temp.update(unwrap(dic[i]))
        else: temp.update({i: dic[i]})
    return temp

temp = [{'label': '01', 'status': 'OK', 'capacity': '419 GiB', 'fault_tolerance': 'RAID 1/RAID 1+0', 'logical_drive_type': 'Data LUN', 'encryption_status': 'Not Encrypted', 'physical_drives': [{'label': 'Port 1I Box 1 Bay 1', 'status': 'OK', 'serial_number': 'SXXXXXXXX', 'model': 'YYYYYY', 'capacity': '419 GiB', 'marketing_capacity': '450 GB', 'location': 'Port 1I Box 1 Bay 1', 'fw_version': 'HPD9', 'drive_configuration': 'Configured', 'encryption_status': 'Not Encrypted', 'media_type': 'HDD'}, {'label': 'Port 1I Box 1 Bay 2', 'status': 'OK', 'serial_number': 'SXXXXXX', 'model': 'YYYYYY', 'capacity': '419 GiB', 'marketing_capacity': '450 GB', 'location': 'Port 1I Box 1 Bay 2', 'fw_version': 'HPD9', 'drive_configuration': 'Configured', 'encryption_status': 'Not Encrypted', 'media_type': 'HDD'}]}]
x = unwrap(temp)
x['marketing_capacity']

There's nothing special about dictionaries in lists. 列表中的字典没有什么特别的。 You already know how to look up nested values - just continue the same principle. 您已经知道如何查找嵌套值-继续相同的原理。

print(logical_drives[0]['physical_drives'][0]['marketing_capacity'])

Try this 尝试这个

import hpilo
ilo = hpilo.Ilo('10.0.0.1', 'USER', 'PASSWORD')
health = ilo.get_embedded_health()
logical_drives = (logical_drives[0]['physical_drives'][0]['marketing_capacity'])
print(logical_drives)

Just loop through it like any other list: 像其他任何列表一样循环遍历它:

drive_data =  health['storage']['Controller on System Board']['logical_drives']

for logical_drive in drive_data:
    for physical_drive in logical_drive['physical_drives']:
       print(physical_drive['marketing_capacity'])

To get all values for 'marketing_capacity' I would use a generic list comprehension. 为了获得'marketing_capacity'的所有值,我将使用通用列表推导。

lst = [{'label': '01', 'status': 'OK', 'capacity': '419 GiB', 'fault_tolerance': 'RAID 1/RAID 1+0', 'logical_drive_type': 'Data LUN', 'encryption_status': 'Not Encrypted', 'physical_drives': [{'label': 'Port 1I Box 1 Bay 1', 'status': 'OK', 'serial_number': 'SXXXXXXXX', 'model': 'YYYYYY', 'capacity': '419 GiB', 'marketing_capacity': '450 GB', 'location': 'Port 1I Box 1 Bay 1', 'fw_version': 'HPD9', 'drive_configuration': 'Configured', 'encryption_status': 'Not Encrypted', 'media_type': 'HDD'}, {'label': 'Port 1I Box 1 Bay 2', 'status': 'OK', 'serial_number': 'SXXXXXX', 'model': 'YYYYYY', 'capacity': '419 GiB', 'marketing_capacity': '450 GB', 'location': 'Port 1I Box 1 Bay 2', 'fw_version': 'HPD9', 'drive_configuration': 'Configured', 'encryption_status': 'Not Encrypted', 'media_type': 'HDD'}]}]

res = [d2.get('marketing_capacity') for d2 in d.get('physical_drives') for d in lst]

# ['450 GB', '450 GB']

Note this will work for arbitrary dictionaries in your outer list, as well as arbitrary dictionaries in your inner list. 请注意,这将适用于外部列表中的任意词典以及内部列表中的任意词典。

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

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