我在python中有这个数组数组。
staff = [
['staff1', '2/18/1998', 21.63],
['staff2', '7/7/1999', 15.87],
['staff3', '8/26/2004', 123.46],
]
我有一个用于指定列的数组。
out_columns = ['Name','Date','Profit']
基于这两个数组,我想将它们转换成一个像这样的字典数组;
dict_arr = [
{"Name":"staff1",
"Date":"2/18/1998",
"Profit":21.63
},
{"Name": "staff2",
"Date": "7/7/1999",
"Profit": 15.87
},
{"Name": "staff3",
"Date": "8/26/2004",
"Profit": 123.46
},
]
我正在使用python v3.6
答案 0 :(得分:3)
使用zip函数:
nl = []
for zz in staff:
aa = {}
for key,val in zip(out_columns,zz):
aa[key]= val
nl.append( aa )
print(nl) # [{'Name': 'staff1', 'Date': '2/18/1998', 'Profit': 21.63} , ... ]