numpy.append#
- numpy.append(arr, values, axis=None)[源代码]#
将值追加到数组的末尾.
- 参数:
- arrarray_like
值将追加到此数组的副本.
- valuesarray_like
这些值将追加到 arr 的副本.它必须具有正确的形状(与 arr 相同的形状,不包括 axis ).如果未指定 axis ,则 values 可以是任何形状,并且在使用前会被展平.
- 轴int, optional
将 values 追加到的轴.如果未给出 axis ,则在使用前会展平 arr 和 values .
- 返回:
- appendndarray
将 values 追加到 axis 的 arr 的副本. 请注意,
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