numpy.char.chararray.tolist#
method
- char.chararray.tolist()#
将数组作为
a.ndim层的Python标量的嵌套列表返回.返回数组数据的(嵌套)Python 列表副本.数据项通过
item函数转换为最接近的兼容内置 Python 类型.如果
a.ndim为 0,则由于嵌套列表的深度为 0,因此它根本不会是列表,而是一个简单的 Python 标量.- 参数:
- none
- 返回:
- yobject,或 object 列表,或 object 列表的列表,或 …
数组元素可能嵌套的列表.
注释
可以通过
a = np.array(a.tolist())重新创建数组,尽管这有时会损失精度.示例
对于一维数组,
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'>
此外,对于二维数组,
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