ndarray.tolist():
- 将(多维)数组作为(嵌套)列表返回
- 将数组数据的副本作为(嵌套)Python列表返回
- 数据项将转换为最接近的兼容内置Python类型
- 如果
ndarray.ndim
是0,则返回Python标量
原英文释义如下:
- Return the array as an
ndarray.ndim
-levels deep nested list of
Python scalars - Return a copy of the array data as a (nested)
Python list. - Data items are converted to the nearest compatible
builtin Python type, via the~numpy.ndarray.item
function. - If
ndarray.ndim
is 0, then since the depth of the nested list is 0,
it will not be a list at all, but a simple Python scalar.
import numpy as np
A = array([1, 2, 3])
print(type(A)) # 结果 <class 'numpy.ndarray'>
B = A.tolist() # 结果 [1, 2, 3]
print(B)
print(type(B)) # 结果 <class 'list'>
C = mat(A)
print(C) # 结果 [[1 2 3]]
print(type(C)) # 结果 <class 'numpy.matrix'>
D = C.tolist()
print(D) # 结果 [[1, 2, 3]]
print(type(D)) # 结果 <class 'list'>
E = C.tolist()[0]
print(E) # 结果 [1, 2, 3]
print(type(E)) # 结果 <class 'list'>