欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

list和dict的应用

程序员文章站 2022-04-28 23:06:19
...

list和dict的应用

给定一个list和dict,返回list中元素不存在dict的key、存在dict但是value为空、存在dict但是value为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}})```
    

## 运行结果

list和dict的应用


相关标签: python