在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`函数:
创建一个矩阵matrix = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])获取矩阵的行数num_rows = len(matrix)打印行数print("矩阵的行数为:", num_rows)
以上两种方法都可以用来查看矩阵的行数。`shape`属性返回一个元组,其中第一个元素是矩阵的行数,第二个元素是矩阵的列数。`len`函数直接返回矩阵的行数。
