在Python中,你可以使用`numpy`库来处理矩阵,并查看其行列数。以下是使用`numpy`查看矩阵行列数的方法:
1. 使用`shape`属性:
import numpy as np创建一个矩阵matrix = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])获取矩阵的行数和列数num_rows, num_cols = matrix.shape打印行数和列数print("矩阵的行数为:", num_rows)print("矩阵的列数为:", num_cols)
2. 使用`len`函数:

import numpy as np创建一个矩阵x = np.array([[1, 2, 5], [2, 3, 5], [3, 4, 5], [2, 3, 6]])输出数组的行和列数print(x.shape) 输出:(4, 3)只输出行数print(len(x)) 输出:4只输出列数print(x.shape) 输出:3
3. 使用`ndim`属性:
import numpy as np创建一个矩阵x = np.array([[1, 2, 5], [2, 3, 5], [3, 4, 5], [2, 3, 6]])查看矩阵的维数print(x.ndim) 输出:2,表示这是一个二维矩阵
以上方法可以帮助你获取Python中`numpy`矩阵的行数和列数
