The difference between list() and tolist()

The effect of list() and tolist() is not the same. It was not clear before, but a bug was found and it took half an hour to look for the code.

import numpy as np
x= np.array([[1,2],[1,2]])
x_list=list(x)
x_list
Out: [array([1, 2]), array([1, 2])]

x_tolist=x.tolist()
x_tolist
Out: [[1, 2], [1, 2]]

You can see that list() converts the elements of the np array into a list according to the original format, and the element format of the generated list is still np.array.

type(x_list[0])
Out[42]: numpy.ndarray

And tolist() takes the values ​​in the array as list elements, and the generated list has the same structure as the original array.

Guess you like

Origin blog.csdn.net/weixin_43705953/article/details/108985705