- 列表的元素为一对数据:
strlist = ['a,b','c,d']
dict(map(lambda x:x.split(','),strlist)) #{'a': 'b', 'c': 'd'}
- 用索引:
strlist = ['a', 'b']
dict(zip(range(len(strlist)),strlist)) #{0: 'a', 1: 'b'}
dict(zip(strlist,range(len(strlist)))) #{'a': 0, 'b': 1}
- 嵌套列表:
strlist= [['k1','v1'],['k2','v2']]
dict(strlist) #{'k2': 'v2', 'k1': 'v1'}
- 两个列表:
strlist1=['a', 'b']
strlist2=['k1','k2']
dict(zip(strlist2,strlist1)) #{'k2': 'b', 'k1': 'a'}
- 生成式:
intlist = [1,2,3]
{i:i**2 for i in intlist} #{1: 1, 2: 4, 3: 9}