新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python中如何計算矩陣的行數(shù)?
對于pyhton里面所導(dǎo)入或者定義的矩陣或者表格數(shù)據(jù),想要獲得矩陣的行數(shù)和列數(shù)有以下方法:

1、利用shape函數(shù)輸出矩陣的行和列
x.shape函數(shù)可以輸出一個元組(m,n),其中元組的第一個數(shù)m表示矩陣的行數(shù),元組的第二個數(shù)n為矩陣的列數(shù)
具體代碼如下:
import numpy as np x = np.array([[1,2,5],[2,3,5],[3,4,5],[2,3,6]]) # 輸出數(shù)組的行和列數(shù) print(x.shape) # (4, 3) # 只輸出行數(shù) print(x.shape[0]) # 4 # 只輸出列數(shù) print (x.shape[1]) # 3
2、對于矩陣的行數(shù),也可以使len(x)函數(shù)輸出的矩陣長度,也就是所謂的行數(shù)。
import numpy as np x = np.array([[1,2,5],[2,3,5],[3,4,5],[2,3,6]]) # 輸出數(shù)組的行數(shù) print(len(x)) #4
名稱欄目:創(chuàng)新互聯(lián)Python教程:python中如何計算矩陣的行數(shù)?
鏈接分享:http://m.fisionsoft.com.cn/article/dhpgpoo.html


咨詢
建站咨詢
