如何将两个列表列表转换为字典列表,使第一个元素来自第一个列表,第二个元素来自另一个列表
How to convert two list of lists into list of dictionary such that the first element comes from the first list and the second from the other list
假设我有两个列表如下:
List1=[[1,2],[3,4],[3,8]]
List2=[[1.2,2.4],[2.4,5.0],[4.5,6.0]]
我怎样才能得到上面的内容:
List3 = [{1:1.2,2:2.4},{3:2.4,4:5.0},{3:4.5,8:6.0}]
试试这个:
List3 = [dict(zip(*lsts)) for lsts in zip(List1, List2)]
List1=[[1,2],[3,4],[3,8]]
List2=[[1.2,2.4],[2.4,5.0],[4.5,6.0]]
List3=[]
for i,e in enumerate(List1):
List3.append(dict(zip(List1[i],List2[i])))
print(List3)
假设我有两个列表如下:
List1=[[1,2],[3,4],[3,8]]
List2=[[1.2,2.4],[2.4,5.0],[4.5,6.0]]
我怎样才能得到上面的内容:
List3 = [{1:1.2,2:2.4},{3:2.4,4:5.0},{3:4.5,8:6.0}]
试试这个:
List3 = [dict(zip(*lsts)) for lsts in zip(List1, List2)]
List1=[[1,2],[3,4],[3,8]]
List2=[[1.2,2.4],[2.4,5.0],[4.5,6.0]]
List3=[]
for i,e in enumerate(List1):
List3.append(dict(zip(List1[i],List2[i])))
print(List3)