Home > other >  The dictionary value of none
The dictionary value of none

Time:09-20

Is there a list of the following
[{'id': 1, 'name': 'pool', 'title': None, 'hostId': None, 'hostName': None, 'clusterId': None, 'clusterName': None, 'hpId': None, 'hpName': None, 'status': None}, {'id': 20, 'name': 'test', 'title': None, 'hostId': None, 'hostName': None, 'clusterId': None, 'clusterName': None, 'hpId': None, 'hpName': None, 'status': None}, None, {'id': None, 'name': None, 'title': None, 'hostId': None, 'hostName': None, 'clusterId': None, 'clusterName': None, 'hpId': None, 'hpName': None, 'status': 'false'}, None, None]
Part of the value of the name in the dictionary to none, at the time of traverse to obtain the value of the name, will have the following error
For I in datas:
If I/" name "==pool_name: \ \ judge the value of the name is consistent with the one into the reference
Print (I (" id "))
An error as below
Traceback (the most recent call last) :
The File "D:/interface - test - python/yunziyuan/pool. Py", line 32, in & lt; module>
Get_poolId (" pool ")
The File "D:/interface - test - python/yunziyuan/pool. Py", 30, the line in get_poolId
If I/" name "==pool_name:
TypeError: 'NoneType' object is not subscriptable
Should be a value of None of these problems, this mistake how to deal with more appropriate?

CodePudding user response:

For I in datas:

Instead of

For I in the filter (lambda x: x is not None, datas) :

CodePudding user response:

 

datas=[{'id': 1, 'name': 'pool', 'title': None, 'hostId': None, 'hostName': None, 'clusterId': None, 'clusterName': None, 'hpId': None, 'hpName': None, 'status': None}, {'id': 20, 'name': 'test', 'title': None, 'hostId': None, 'hostName': None, 'clusterId': None, 'clusterName': None, 'hpId': None, 'hpName': None, 'status': None}, None, {'id': None, 'name': None, 'title': None, 'hostId': None, 'hostName': None, 'clusterId': None, 'clusterName': None, 'hpId': None, 'hpName': None, 'status': 'false'}, None, None]
For I in datas:
If I!=None:
If I [' name ']=='pool' :
Print (I [' id '])
  • Related