程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Mutual conversion of ndarray object and list in Python

編輯:Python

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved