在Python中,可以使用不同的方法來遍歷二維數(shù)組,以下是一些常用的方法:
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for i in range(len(arr)):
for j in range(len(arr[i])):
print(arr[i][j])
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[print(num) for sublist in arr for num in sublist]
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
for row in arr:
for num in row:
print(num)
import itertools
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for num in itertools.chain(*arr):
print(num)
這些是一些常用的方法來遍歷二維數(shù)組,根據(jù)具體情況選擇合適的方法來處理二維數(shù)組。