當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python numpy append用法及代碼示例

本文簡要介紹 python 語言中 numpy.append 的用法。

用法:

numpy.append(arr, values, axis=None)

將值附加到數組的末尾。

參數

arr array_like

值將附加到此數組的副本中。

values array_like

這些值附加到 arr 的副本中。它必須具有正確的形狀(與 arr 形狀相同,不包括軸)。如果未指定軸,則值可以是任何形狀,並且在使用前會被展平。

axis 整數,可選

附加值的軸。如果未給出軸,則 arr 和 values 在使用前都會被展平。

返回

append ndarray

一份arr附加到.注意append不會就地發生:分配並填充一個新數組。如果是無,out是一個扁平數組。

例子

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

指定軸時,值必須具有正確的形狀。

>>> 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)

相關用法


注:本文由純淨天空篩選整理自numpy.org大神的英文原創作品 numpy.append。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。