Python There are many functions of the list , however ndarray Objects are more powerful .
We will use both , They can all be seen as a form of matrix , Therefore, it is necessary to understand the mutual conversion operation between them .
About ndarray Basic introduction of object , You can refer to the blog post :
https://blog.csdn.net/wenhao_ir/article/details/124416798
About list( list ) Detailed introduction , You can refer to the blog post :
https://blog.csdn.net/wenhao_ir/article/details/125400072
Next , First look at ndarray How to convert an object to list object .
It can be used ndarray Object method tolist() Implement transformation .
The sample code is as follows :
import numpy as np
ndarray1 = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]], dtype='int8')
list1 = ndarray1.tolist()
The operation results are as follows :
From the above running results, we can see that :
ndarray1 Each row of is converted to a list , Stored in list1 in .
Let's see list How to convert an object to ndarray object .
use ndarray Object method array() That is to say .
The sample code is as follows :
import numpy as np
list1 = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]
ndarray1 = np.array(list1)
The operation results are as follows :
From the above conversion results, we can see , list (list) Each list element in the is converted to ndarray A row in the .