Skip to main content
 首页 » 编程设计

python之如何 reshape 列表数组

2025年02月15日652php

我有以下数组:

 array([ list(['4.224609607748e+00', '4.085857782359e+00', '1.273383604708e+00', '-6.802974691818e-03']), 
   list(['9.099528133406e-03', '6.867550219273e-03', '5.859231188647e-03', '3.684441849425e-03']), 
   list(['5.836775773317e-04', '-2.316776085880e-16', '-1.456850991492e-16', '-2.307897076406e-17']), 
   list(['4.140895678156e-03', '2.603906355541e-03', '4.125025757803e-04', '-1.739011495381e-03']), 
   list(['-1.681896173898e-03', '-5.241735641835e-04', '-1.739011375813e-03', '-1.681896058258e-03']), dtype=object) 

我想对其进行修改,使其不再是一个包含 6 个列表的数组,而是一个形状为 (6,4) 的数组(即每个 float 都是数组中的一个元素)。我还想将 dtype 更改为 float(我不能这样做,因为它不会将列表识别为 float)。

我有办法做到这一点吗?

谢谢!

请您参考如下方法:

你只需要:

a = np.asarray([list(map(float, item)) for item in a])