在Python中,矩阵遍历有多种方法,以下是几种常见的方法:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for i in range(len(matrix)):
for j in range(len(matrix[i])):
print(matrix[i][j])
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
elements = [element for row in matrix for element in row]
print(elements)
import numpy as np
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
np_matrix = np.array(matrix)
for row in np_matrix:
for element in row:
print(element)
import numpy as np
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
np_matrix = np.array(matrix)
for element in np_matrix.flat:
print(element)
这些方法可以根据具体需求选择适合的方式遍历矩阵。