Python 两个list合并成一个字典
2021-02-13 06:18
标签:lambda ict print zip int col 方法 lis item Python 两个list合并成一个字典 标签:lambda ict print zip int col 方法 lis item 原文地址:https://www.cnblogs.com/brf-test/p/12728115.html方法一:
list1 = [‘k1‘,‘k2‘,‘k3‘]
list2 = [‘v1‘,‘v2‘,‘v3‘]
dic = dict(map(lambda x,y:[x,y],list1,list2))
>>> print(dic)
{‘k3‘: ‘v3‘, ‘k2‘: ‘v2‘, ‘k1‘: ‘v1‘}
方法二:
>>> dict(zip(list1,list2))
{‘k3‘: ‘v3‘, ‘k2‘: ‘v2‘, ‘k1‘: ‘v1‘}
>>> l1=[1,2,3,4,5,6]
>>> l2=[4,5,6,7,8,9]
>>>
>>>
>>>
>>> {k:v for k,v in zip(l1,l2)}
{1: 4, 2: 5, 3: 6, 4: 7, 5: 8, 6: 9}
>>>
>>>
>>>
>>> x = {1: 4, 2: 5, 3: 6, 4: 7, 5: 8, 6: 9}
>>> {v:k for k,v in x.items()} #反过来 将字典中的v和k调换
{4: 1, 5: 2, 6: 3, 7: 4, 8: 5, 9: 6}
上一篇:4.K均值算法--应用
下一篇:Java 提升 01
文章标题:Python 两个list合并成一个字典
文章链接:http://soscw.com/index.php/essay/54774.html