Python3 列表,數(shù)組,矩陣的相互轉(zhuǎn)換的方法示例
在使用列表、數(shù)組和矩陣的過程中,經(jīng)常需要相互轉(zhuǎn)換。特此總結(jié)相互間轉(zhuǎn)換的過程及結(jié)果,供大家參考。
第三方包:numpy
import numpy as np mylist = [[1, 2, 3], [4, 5, 6]] # 列表 print(type(mylist)) print(mylist, end='\n\n') myarray = np.array(mylist) # 列表轉(zhuǎn)數(shù)組 print(type(myarray)) print(myarray, end="\n\n") mymatrix = np.mat(mylist) # 列表轉(zhuǎn)矩陣 print(type(mymatrix)) print(mymatrix, end='\n\n') MatToArray = np.array(mymatrix) # 矩陣轉(zhuǎn)數(shù)組 print(type(MatToArray)) print(MatToArray, end='\n\n') ArrayToMat = np.mat(myarray) # 數(shù)組轉(zhuǎn)矩陣 print(type(ArrayToMat)) print(ArrayToMat, end='\n\n') MatToList1 = mymatrix.tolist() # 矩陣轉(zhuǎn)列表 print(type(MatToList1)) print(MatToList1) MatToList2 = list(mymatrix) # 注意點1 print(type(MatToList2)) print(MatToList2, end='\n\n') ArrayToList1 = myarray.tolist() # 矩陣轉(zhuǎn)列表 print(type(ArrayToList1)) print(ArrayToList1) ArrayToList2 = list(myarray) # 注意點2 print(type(ArrayToList2)) print(ArrayToList2)
函數(shù)運行結(jié)果顯示如下。注意一點是,最后的矩陣和數(shù)組轉(zhuǎn)換成列表形式,用list()是將矩陣和數(shù)組整體轉(zhuǎn)換成列表。如果要將其轉(zhuǎn)換成基本的列表形式,則需要使用<array>.tolist() 或者 <matrix>.tolist()來轉(zhuǎn)換。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python中列表(List) 的三種遍歷(序號和值)方法小結(jié)
這篇文章主要介紹了Python中列表(List) 的三種遍歷(序號和值)方法小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05Python如何應(yīng)用cx_Oracle獲取oracle中的clob字段問題
今天小編就為大家分享一篇Python如何應(yīng)用cx_Oracle獲取oracle中的clob字段問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python3 用matplotlib繪制sigmoid函數(shù)的案例
這篇文章主要介紹了Python3 用matplotlib繪制sigmoid函數(shù)的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12