numpy.append#
- numpy.append(arr, values, axis=None)[源代码]#
将值附加到数组的末尾。
- 参数:
- arrarray_like
值将被追加到该数组的副本中。
- valuesarray_like
这些值将被追加到 arr 的副本中。它必须具有正确的形状(与 arr 相同的形状,排除 axis)。如果未指定 axis,则 values 可以是任何形状,并将在使用前展平。
- axisint, optional
将 values 追加到的轴。如果未给出 axis,则将在使用前展平 arr 和 values。
- 返回:
- appendndarray
arr 的副本,将 values 追加到 axis。请注意,
append不是就地操作:而是分配并填充一个新数组。如果 axis 为 None,则 out 是一个展平的数组。
示例
>>> import numpy as np >>> 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)
>>> a = np.array([1, 2], dtype=int) >>> c = np.append(a, []) >>> c array([1., 2.]) >>> c.dtype float64
空 ndarray 的默认 dtype 为
float64,因此当与 dtypeint64结合时,输出的 dtype 为float64