numpy.char.chararray.tolist#
方法
- char.chararray.tolist()#
以 Python 纯量形式返回陣列,作為
a.ndim
層級的深度巢狀列表。以(巢狀)Python 列表形式返回陣列資料的副本。資料項目會透過
item
函式,轉換為最接近的相容內建 Python 類型。如果
a.ndim
為 0,則由於巢狀列表的深度為 0,因此它根本不會是列表,而只會是簡單的 Python 纯量。- 參數:
- 無
- 返回值:
- y物件,或物件列表,或物件列表的列表,或 …
陣列元素可能為巢狀的列表。
註解
陣列可能會透過
a = np.array(a.tolist())
重新建立,但有時可能會損失精確度。範例
對於 1D 陣列,
a.tolist()
幾乎與list(a)
相同,不同之處在於tolist
會將 numpy 纯量變更為 Python 纯量>>> import numpy as np >>> a = np.uint32([1, 2]) >>> a_list = list(a) >>> a_list [np.uint32(1), np.uint32(2)] >>> type(a_list[0]) <class 'numpy.uint32'> >>> a_tolist = a.tolist() >>> a_tolist [1, 2] >>> type(a_tolist[0]) <class 'int'>
此外,對於 2D 陣列,
tolist
會以遞迴方式套用>>> a = np.array([[1, 2], [3, 4]]) >>> list(a) [array([1, 2]), array([3, 4])] >>> a.tolist() [[1, 2], [3, 4]]
此遞迴的基本情況是 0D 陣列
>>> a = np.array(1) >>> list(a) Traceback (most recent call last): ... TypeError: iteration over a 0-d array >>> a.tolist() 1