Car*_*tem 3 python dictionary list
刚开始使用python并且知道足够知道我什么都不知道.我想找到将列表拆分为dicts列表的替代方法.示例列表:
data = ['ID:0:0:0',
'Status:Ok',
'Name:PhysicalDisk0:0:0',
'State:Online',
'FailurePredicted:No',
'ID:0:0:1',
'Status:Ok',
'Name:PhysicalDisk0:0:1',
'State:Online',
'FailurePredicted:No']
Run Code Online (Sandbox Code Playgroud)
完成的dicts列表:
[{'Status': 'Ok',
'State': 'Online',
'ID': '0:0:0',
'FailurePredicted': 'No',
'Name': 'PhysicalDisk0:0:0'},
{'Status': 'Ok',
'State': 'Online',
'ID': '0:0:1',
'Name': 'PhysicalDisk0:0:1',
'FailurePredicted': 'No'}]
Run Code Online (Sandbox Code Playgroud)
该列表具有需要多个dicts的重复元素,并且列表的长度不同.我的代码似乎可以简化,只要我更了解Python.我目前的代码:
删除的代码它没有用.:(
----------- File output as requested -------------------
# omreport storage pdisk controller=0
List of Physical Disks on Controller PERC 5/i Integrated (Embedded)
Controller PERC 5/i Integrated (Embedded)
ID : 0:0:0
Status : Ok
Name : Physical Disk 0:0:0
State : Online
Failure Predicted : No
Progress : Not Applicable
Type : SAS
Capacity : 136.13 GB (146163105792 bytes)
Used RAID Disk Space : 136.13 GB (146163105792 bytes)
Available RAID Disk Space : 0.00 GB (0 bytes)
Hot Spare : No
Vendor ID : DELL
Product ID : ST3146755SS
Revision : T107
Serial No. : 3LN1EF0G
Negotiated Speed : Not Available
Capable Speed : Not Available
Manufacture Day : 07
Manufacture Week : 24
Manufacture Year : 2005
SAS Address : 5000C50004731C35
ID : 0:0:1
Status : Ok
Name : Physical Disk 0:0:1
State : Online
Failure Predicted : No
Progress : Not Applicable
Type : SAS
Capacity : 136.13 GB (146163105792 bytes)
Used RAID Disk Space : 136.13 GB (146163105792 bytes)
Available RAID Disk Space : 0.00 GB (0 bytes)
Hot Spare : No
Vendor ID : DELL
Product ID : ST3146755SS
Revision : T107
Serial No. : 3LN1EF88
Negotiated Speed : Not Available
Capable Speed : Not Available
Manufacture Day : 07
Manufacture Week : 24
Manufacture Year : 2005
SAS Address : 5000C500047320B9
Run Code Online (Sandbox Code Playgroud)
result = [{}]
for item in data:
key, val = item.split(":", 1)
if key in result[-1]:
result.append({})
result[-1][key] = val
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7239 次 |
| 最近记录: |