목록 및 사전 적용
목록과 dict가 주어지면 목록의 요소 키가 dict에 존재하지 않고 dict가 있지만 값이 비어 있고 dict가 있지만 값이 0이라는 결과를 반환합니다.
직접 코딩
def isnotexsit(dict1, list1):
'''
给一个字典,列表,返回字典对应key不存在列表的结果集
:param dict: {},[]
:return: []
'''
# 获取dict对应可以
list2 = []
list3 = []
dict_key = dict1.keys()
for i in list1:
if i in dict_key:
list3.append(i)
else:
list2.append(i)
return {'not': list2, 'yes': list3}
def isnull(dict1, list1):
'''
获取dict中value为null的结果
:param dict:
:return: []
'''
l = isnotexsit(dict1, list1)
k = l.get('yes')
list1 = []
list2=[]
for i in k:
a = dict1.get(i)
if str(a).strip()=='':
list1.append(i)
elif int(dict1[i])==0:
list2.append(i)
return list1,list2
def result(dict1, list1):
'''
数据组装
:param dict1:
:param list1:
:return: 不存在list,为空list,为0 list
'''
a = isnotexsit(dict1, list1)
nexsit = a.get('not')
null_exsit,exit_0 = isnull(dict1, list1)
return nexsit,null_exsit,exit_0
디버그
if __name__ == '__main__':
dict1={"faceLivenessResult": {
"thresholdE4": '',
"thresholdE5":' ',
"thresholdE3": 65.3,
"intermediaryDeviceNum": 0
},
"userRelationInfo": {
"pyramidSaleCountD1": '',
"pyramidSaleCountD2": 0,
"asAgent2Num": 0,
"intermediaryDeviceNum": 0
}}
dict2={"faceLivenessResult": [
"thresholdE4",
"thresholdE5",
"thresholdE3",
"intermediaryDeviceNum",
"a"
],
"userRelationInfo": [
"pyramidSaleCountD1",
"pyramidSaleCountD2",
"asAgent2Num",
"intermediaryDeviceNum"
]}
for key in dict2.keys():
nexsit,null_exsit,exit_0=result(dict1[key],dict2[key])
print({key:{"不存在的key":nexsit, "结果为空的":null_exsit, "结果为0的": exit_0}})```
## 运行结果