【转】Python 列表转为字典的两个小方法
1、现在有两个列表,list1 = ['key1','key2','key3']和list2 = ['1','2','3'],把他们转为这样的字典:{'key1':'1','key2':'2','key3':'3'}
list1 = ['key1','key2','key3']
list2 = ['1','2','3']
dict(zip(list1,list2))
2、将嵌套列表转为字典,有两种方法,
new_list= [['key1','value1'],['key2','value2'],['key3','value3']]
dict(list)
或者这样:
new_list= [['key1','value1'],['key2','value2'],['key3','value3']]
new_dict = {}
for i in new_list:
... new_dict[i[0]] = i[1] #字典赋值,左边为key,右边为value
...
new_dict
【转自:】http://www.cnblogs.com/iwangzc/p/4031085.html?utm_source=tuicool