numpy.append

numpy.append(arr, values, axis=None)[源代码]

将值追加到数组的末尾。

参数
arrarray_like

值将附加到此数组的副本中。

valuesarray_like

这些值将附加到 arr . 它的形状必须正确(与 arr 除外 axis )如果 axis 未指定, values 可以是任何形状,使用前将被压扁。

axis可选的

沿着哪个轴 values 被追加。如果 axis 两个都不给 arrvalues 在使用前被压扁。

返回
append恩达雷

一份 arr 具有 values 追加到 axis .注意 append 未就地出现:分配并填充了新数组。如果 axis 没有, out 是扁平数组。

参见

insert

将元素插入数组。

delete

从数组中删除元素。

实例

>>> np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]])
array([1, 2, 3, ..., 7, 8, 9])

什么时候? axis 已指定, values 必须具有正确的形状。

>>> np.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9]], axis=0)
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> np.append([[1, 2, 3], [4, 5, 6]], [7, 8, 9], axis=0)
Traceback (most recent call last):
    ...
ValueError: all the input arrays must have same number of dimensions, but
the array at index 0 has 2 dimension(s) and the array at index 1 has 1
dimension(s)