1、现在有两个列表,list1 = ['key1','key2','key3']和list2 = ['1','2','3'],把他们转为这样的字典:{'key1':'1','key2':'2','key3':'3'}
list1= ['key1','key2','key3']
list2= ['1','2','3']
new_dict1=dict(zip(list1,list2))
print(new_dict1)
2,将嵌套列表转为字典,有两种方法,
new_list=[['key1','value1'],['key2','value2'],['key3','value3']]
#方法1
new_dict2=dict(new_list)
print(new_dict2)
#方法2
new_dict201={}
for iin new_list:
new_dict201[i[0]]=i[1]
print(new_dict201)
网友评论