相关文章推荐
乖乖的韭菜  ·  Electron Microscopy ...·  9 月前    · 
乖乖的韭菜  ·  nodejs gzip ...·  9 月前    · 
乖乖的韭菜  ·  HBuilderX.vue文件 以及 ...·  10 月前    · 
乖乖的韭菜  ·  CSS3变形 ...·  10 月前    · 
乖乖的韭菜  ·  Uploading to Steam ...·  10 月前    · 
销魂的大白菜  ·  pycharm报错:ModuleNotFou ...·  12 分钟前    · 
神勇威武的长颈鹿  ·  python ...·  12 分钟前    · 
会开车的企鹅  ·  python ...·  12 分钟前    · 
ndarray. tolist ( ) #

Return the array as an a.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 item function.

If a.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.

Parameters :
Returns :
y object, or list of object, or list of list of object, or …

The possibly nested list of array elements.

Notes

The array may be recreated via a = np.array(a.tolist()) , although this may sometimes lose precision.

Examples

For a 1D array, a.tolist() is almost the same as list(a) , except that tolist changes numpy scalars to Python scalars:

>>> a = np.uint32([1, 2])
>>> a_list = list(a)
>>> a_list
[1, 2]
>>> type(a_list[0])
<class 'numpy.uint32'>
>>> a_tolist = a.tolist()
>>> a_tolist
[1, 2]
>>> type(a_tolist[0])
<class 'int'>

Additionally, for a 2D array, tolist applies recursively:

>>> a = np.array([[1, 2], [3, 4]])
>>> list(a)
[array([1, 2]), array([3, 4])]
>>> a.tolist()
[[1, 2], [3, 4]]

The base case for this recursion is a 0D array:

>>> a = np.array(1)
>>> list(a)
Traceback (most recent call last):
TypeError: iteration over a 0-d array
>>> a.tolist()
 
推荐文章