繁体   English   中英

在numpy的二维数组中的特定位置插入一行?

[英]Inserting a row at a specific location in a 2d array in numpy?

我在 numpy 中有一个二维数组,我想在其中插入一个新行。 以下问题Numpy - add row to array可以提供帮助。 我们可以使用numpy.vstack ,但它在开始或结束时堆叠。 任何人都可以在这方面提供帮助。

您可能正在寻找numpy.insert

>>> import numpy as np
>>> a = np.zeros((2, 2))
>>> a
array([[ 0.,  0.],
       [ 0.,  0.]])
# In the following line 1 is the index before which to insert, 0 is the axis.
>>> np.insert(a, 1, np.array((1, 1)), 0)  
array([[ 0.,  0.],
       [ 1.,  1.],
       [ 0.,  0.]])
>>> np.insert(a, 1, np.array((1, 1)), 1)
array([[ 0.,  1.,  0.],
       [ 0.,  1.,  0.]])

该插件似乎不适用于我。 Iam使用numpy 1.17.2。

但是,如果您想插入行,这对我有用

array[index] = (1,1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM