一、如今有兩個列表,list1 = ['key1','key2','key3']和list2 = ['1','2','3'],把他們轉爲這樣的字典:{'key1':'1','key2':'2','key3':'3'}post
>>>list1 = ['key1','key2','key3']blog
>>>list2 = ['1','2','3']ip
>>>dict(zip(list1,list2))class
{'key1':'1','key2':'2','key3':'3'}方法
二、將嵌套列表轉爲字典,有兩種方法,dict
>>>new_list= [['key1','value1'],['key2','value2'],['key3','value3']]di
>>>dict(list)字典
{'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}new
或者這樣:
>>>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
{'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}