在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)
這些方法可以根據具體需求選擇適合的方式遍歷矩陣。