download:??JavaEE在線就業班2.0【最新升級版】??
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))
{key1:1,key2:2,key3:3}
2、將嵌套列表轉為字典,有兩種辦法,
>>>new_list= [[key1,value1],[key2,value2],[key3,value3]]
>>>dict(list)
{key3: value3, key2: value2, key1: value1}
或者這樣:
>>>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}