Python中把列表转换为字典
有以下几个方法:
1、 现在有两个列表,list1 = ['key1','key2','key3']
和list2 = ['1','2','3']
,把他们转为这样的字典:{'key1':'1','key2':'2','key3':'3'}
1 | list1 = ['key1','key2','key3'] |
2、 将嵌套列表转为字典,有两种方法
1 | new_list= [['key1','value1'],['key2','value2'],['key3','value3']] |
1 | new_list= [['key1','value1'],['key2','value2'],['key3','value3']] |
3、 list1 = [k1 : {key1 : value1, key2 : value3}] 转变成 list2 = [k1: [ {key1 : value1}, {key2 : value2}] ]
1 | for k, v in list1.items: |