3 回答
TA贡献1812条经验 获得超5个赞
from numpy import transpose
a1 = ["a", "b", "c"]
a2 = ["1,20,300", "2,10,300", "3,40,300"]
a2t = transpose([e.split(",") for e in a2])
result = {a1[i] : list(a2t[i]) for i in range(len(a1))}
=> {'a': ['1', '2', '3'], 'b': ['20', '10', '40'], 'c': ['300', '300', '300']}
感谢 Code-Apprentice 建议使用 {x : y for ... }
TA贡献1802条经验 获得超5个赞
使用map, split, numpy 数组转置zip和dict
n = np.array(list(map(lambda x: x.split(','), a2))).T.tolist()
Out[245]: [['1', '2', '3'], ['20', '10', '40'], ['300', '300', '300']]
result = dict(zip(a1, n))
Out[247]: {'a': ['1', '2', '3'], 'b': ['20', '10', '40'], 'c': ['300', '300', '3
00']}
TA贡献1830条经验 获得超9个赞
a1 = ["a", "b", "c"]
a2 = ["1,20,300", "2,10,300", "3,40,300"]
a2 = [item.split(',') for item in a2]
res = {}
for i in range(len(a1)):
res[a1[i]] = [item[i] for item in a2]
res
{'a': ['1', '2', '3'], 'b': ['20', '10', '40'], 'c': ['300', '300', '300']}
添加回答
举报