我在Numpy数组中有2列数据,我想从值对中创建字典。数据看起来像这样-
[[0.000e+00 1.510e+02]
[1.000e+00 1.410e+02]
[2.000e+00 1.490e+02]
...
[2.238e+03 1.580e+02]
[2.239e+03 1.520e+02]
[2.240e+03 1.620e+02]]
代码为:
data = np.loadtxt('neon_spectrum.txt', delimiter=' ') # Loads data into Numpy array as shown above
neon_dictionary = {}
for (x, y) in data:
neon_dictionary[y] = x
print(neon_dictionary)
预期结果:{151.0:0.0, 141.0:1.0, 149.0:2.0 ... 162.0:2240.0}
实际结果(键是正确的,值是垃圾):{151.0:265.0, 141.0:539.0, ...}
无错误消息。我无法弄清楚我在词典分配中犯了什么错误。谢谢。