zip函数组装字典和列表

发布时间 2023-04-27 14:19:37作者: Titen

zip:字面意思就是打包的意思,目的也是打包

组装列表:

def get_dict(a:list,b:list):
    d = list(zip(a,b))
    return d


if __name__ == '__main__':
    # print(time_specific())
    list1 = [1,2,3]
    list2 = [4,5,6]
    print(get_dict(list1,list2))

结果:

D:\Python\python.exe D:\highway\api\highway_apiframe\commons\wz.py 
[(1, 4), (2, 5), (3, 6)]

进程已结束,退出代码0

 

组装字典:

def get_dict(a:list,b:list):
    d = dict(zip(a,b))
    return d


if __name__ == '__main__':
    # print(time_specific())
    list1 = [1,2,3]
    list2 = [4,5,6]
    print(get_dict(list1,list2))

结果:

D:\Python\python.exe D:\highway\api\highway_apiframe\commons\wz.py 
{1: 4, 2: 5, 3: 6}

进程已结束,退出代码0