Python 在numpy数组中插入numpy.float64元素

Python 在numpy数组中插入numpy.float64元素,python,numpy,Python,Numpy,我正在尝试在numpy数组中插入numpy float。 代码和输出为: dos = np.sum(atom[:, :, 1:],axis=0) print("type(dos)") print(type(dos)) print("dos.shape") print(dos.shape) print("dos[15] Before") print(dos[15]) print("type(atom[1,0,0])") print(type(atom[1,0,0])) print("atom[1,

我正在尝试在numpy数组中插入numpy float。 代码和输出为:

dos = np.sum(atom[:, :, 1:],axis=0)
print("type(dos)")
print(type(dos))
print("dos.shape")
print(dos.shape)
print("dos[15] Before")
print(dos[15])
print("type(atom[1,0,0])")
print(type(atom[1,0,0]))
print("atom[1,0,0]")
print(atom[1,0,0])
for i in range(301):
    dos2=np.insert(dos, 0, atom[1,0,0])
print("dos[15] After ")
print(dos2[15])
print("type(dos2)")
print(type(dos2))
type(dos)
<class 'numpy.ndarray'>
dos.shape
(301, 18)
dos[15] Before
[ -9.75080030e-02  -8.37110240e-02  -3.13760517e-03  -2.70089494e-03
  -2.07915835e-03  -1.77532740e-03  -2.03548911e-03  -1.73346437e-03
  -1.98000973e-04  -1.64015415e-04  -1.99115166e-04  -1.65569761e-04
  -9.07381374e-05  -7.37546825e-05  -1.48250176e-04  -1.22108731e-04
  -1.18854648e-04  -9.70416840e-05]
type(atom[1,0,0])
<class 'numpy.float64'>
atom[1,0,0]
-4.11
dos[15] After 
0.0
type(dos2)
<class 'numpy.ndarray'>
相应的输出为:

dos = np.sum(atom[:, :, 1:],axis=0)
print("type(dos)")
print(type(dos))
print("dos.shape")
print(dos.shape)
print("dos[15] Before")
print(dos[15])
print("type(atom[1,0,0])")
print(type(atom[1,0,0]))
print("atom[1,0,0]")
print(atom[1,0,0])
for i in range(301):
    dos2=np.insert(dos, 0, atom[1,0,0])
print("dos[15] After ")
print(dos2[15])
print("type(dos2)")
print(type(dos2))
type(dos)
<class 'numpy.ndarray'>
dos.shape
(301, 18)
dos[15] Before
[ -9.75080030e-02  -8.37110240e-02  -3.13760517e-03  -2.70089494e-03
  -2.07915835e-03  -1.77532740e-03  -2.03548911e-03  -1.73346437e-03
  -1.98000973e-04  -1.64015415e-04  -1.99115166e-04  -1.65569761e-04
  -9.07381374e-05  -7.37546825e-05  -1.48250176e-04  -1.22108731e-04
  -1.18854648e-04  -9.70416840e-05]
type(atom[1,0,0])
<class 'numpy.float64'>
atom[1,0,0]
-4.11
dos[15] After 
0.0
type(dos2)
<class 'numpy.ndarray'>
从裸体上看,我看不出哪里出了问题。
请帮忙。

从提到的文件:

插入值的arr副本。请注意,insert不会就地执行:返回一个新数组。如果“轴”为“无”,则“输出”为展平阵列

这意味着您的循环:

for i in range(301):
    dos2=np.insert(dos, 0, atom[1,0,0])
执行
300
无用操作,然后插入单个值,
dos2
包含
dos
301*18
值加上一个值(展平):

您可能希望将该值应用于dos中的每个元素:

也可以简单地表示为:

>>> dos2 = np.empty((dos.shape[0], dos.shape[1] + 1), dtype=dos.dtype)
>>> dos2[:, 0] = 12
>>> dos2[:, 1:] = dos

从提到的文件中:

插入值的arr副本。请注意,insert不会就地执行:返回一个新数组。如果“轴”为“无”,则“输出”为展平阵列

这意味着您的循环:

for i in range(301):
    dos2=np.insert(dos, 0, atom[1,0,0])
执行
300
无用操作,然后插入单个值,
dos2
包含
dos
301*18
值加上一个值(展平):

您可能希望将该值应用于dos中的每个元素:

也可以简单地表示为:

>>> dos2 = np.empty((dos.shape[0], dos.shape[1] + 1), dtype=dos.dtype)
>>> dos2[:, 0] = 12
>>> dos2[:, 1:] = dos

从“预期结果”来看,您似乎只想在已经平坦的数组的开头插入一个值。绝对不需要使用for循环来实现这一点

>>> insert_value = 100
>>> orig_array = np.array([1, 2, 3, 4, 5])
>>> final_array = np.insert(orig_array, 0, insert_value)
>>> print(final_array)
[100   1   2   3   4   5]

从“预期结果”来看,您似乎只想在已经平坦的数组的开头插入一个值。绝对不需要使用for循环来实现这一点

>>> insert_value = 100
>>> orig_array = np.array([1, 2, 3, 4, 5])
>>> final_array = np.insert(orig_array, 0, insert_value)
>>> print(final_array)
[100   1   2   3   4   5]