将元素添加到 python Numpy 中的指定索引
Adding elements to a specified index in python Numpy
如何在具有指定索引的数组中放置一个值。就像我如何将 number
3 放在 array
.
中的第 4 个和第 5 个元素之间
number = 3
index= 5
array= np.array([ 31, 28, 31, 30, 31, 30, 31, 31])
预期输出
[ 31, 28, 31, 30, 3, 31, 30, 31, 31]
import numpy as np
np.insert(array, index, number)
您可以使用 numpy.insert
函数在 numpy.array
中的指定点插入一个值。以下是您将如何在您的案例中使用它:
array = np.array([ 31, 28, 31, 30, 31, 30, 31, 31])
np.insert(array, 5, 3)
第二个参数是要在其前插入值的索引,也就是第三个参数。有关更多信息,请参阅 documentation here,尤其是对于高维数组,它可能会变得有点复杂。
如何在具有指定索引的数组中放置一个值。就像我如何将 number
3 放在 array
.
number = 3
index= 5
array= np.array([ 31, 28, 31, 30, 31, 30, 31, 31])
预期输出
[ 31, 28, 31, 30, 3, 31, 30, 31, 31]
import numpy as np
np.insert(array, index, number)
您可以使用 numpy.insert
函数在 numpy.array
中的指定点插入一个值。以下是您将如何在您的案例中使用它:
array = np.array([ 31, 28, 31, 30, 31, 30, 31, 31])
np.insert(array, 5, 3)
第二个参数是要在其前插入值的索引,也就是第三个参数。有关更多信息,请参阅 documentation here,尤其是对于高维数组,它可能会变得有点复杂。