Python列表转换成字典的方法

list1=["key1","key2"]
list2=["value1","value2"]
print(dict(zip(list1,list2)))

运行结果:

 {'key1': 'value1', 'key2': 'value2'}

list=[["key1","key2"],["value1","value2"]]
print(dict(list))

运行结果:

{'key1': 'key2', 'value1': 'value2'}

list=[["key1","key2"],["value1","value2"]]

dict={}

for i in range(len(list)):
    dict[list[0][i]]=list[1][i]

print(dict)

运行结果:

{'key1': 'value1', 'key2': 'value2'}

猜你喜欢

转载自blog.csdn.net/q1246192888/article/details/109710125
今日推荐