这是一个json响应,我从我的Web客户端视图中获取,我想检查这个列表字典是否包含数据。我怎么能继续?谢谢
[
{
"id": 41,
"commercialOffice": false,
"haveStoreRooms": false,
"workFromHome": false,
"seperateOffice": false,
"receptionArea": false,
"standardInsurance": false,
"teamAudit": false,
"auditReport": false,
"receptionist": false,
"qualityAssuranceTeam": false,
"subContractor": null,
"employeeIdentityCard": false,
"employeeUniform": false,
"insuranceList": "",
"company": null
},
{
"id": 45,
"commercialOffice": false,
"haveStoreRooms": false,
"workFromHome": false,
"seperateOffice": false,
"receptionArea": false,
"standardInsurance": false,
"teamAudit": false,
"auditReport": false,
"receptionist": false,
"qualityAssuranceTeam": false,
"subContractor": null,
"employeeIdentityCard": false,
"employeeUniform": false,
"insuranceList": "0",
"company": 71
},
{
"id": 46,
"commercialOffice": true,
"haveStoreRooms": true,
"workFromHome": false,
"seperateOffice": false,
"receptionArea": false,
"standardInsurance": true,
"teamAudit": false,
"auditReport": false,
"receptionist": false,
"qualityAssuranceTeam": false,
"subContractor": null,
"employeeIdentityCard": true,
"employeeUniform": false,
"insuranceList": "0",
"company": 68
},
]
我在想这个
def (self):
dic = {'key1': ['value1', 'value2'],
'key2': 'value77' }
values = dic.values()
'value77' in [x for v in values for x in v if type(v)==list] or
'value77' in values
答案 0 :(得分:1)
这取决于你的数据意味着什么。是dict键吗?它是一个给定的字典键吗?
Python中的空字典是假的,所以只需在列表中调用all()
就可以告诉你每个字典是否至少有一个键值对:
>>> all( [ {1:2}, {'A':'B'} ] )
True
>>> all( [ {1:2}, {'A':'B'}, {}, {3:4}] )
False
如果要检查每个字典中是否存在给定键,可以将列表理解与get
结合使用。
答案 1 :(得分:0)
使用 运算符:
if b in a:
<强>演示:强>
>>> a = {'foo': 1, 'bar': 2}
>>> 'foo' in a
True
>>$gt; 'spam' in a
False
@Eric Duminil解释的那个解决方案可以更好地解决你的问题
根据您的要求更新:
dic_3 = {'key1': ['value1', 'value2'],'key2': '','key3':None }
for key, value in dic_3.iteritems():
if value is None or value =='':
print key ,"is Empty"
<强>输出:强>
key3 is Empty
key2 is Empty