Application of list and dict

Application of list and dict

Given a list and dict, return the result that the key of the element in the list does not exist in the dict, the dict exists but the value is empty, and the dict exists but the value is 0.

Code directly

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

debug

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}})```
    

## 运行结果

Insert code snippet here


Guess you like

Origin blog.csdn.net/kairui_guxiaobai/article/details/104696589