def find_neighbors(matrix, row, col): rows = len(matrix) cols = len(matrix[0]) neighbors = [] # 遍历周围的8个点(包括对角线方向) for i in range(-1, 2): for j in range(-1, 2): # 排除当前点以及超出矩阵边界的点 if i == 0 and j == 0: continue if row + i >= 0 and row + i < rows and col + j >= 0 and col + j < cols: neighbors.append(matrix[row + i][col + j]) return neighbors # 构造10*10的二维矩阵 matrix = [[0] * 10 for i in range(10)] # 遍历矩阵中的每个点,输出其周围点 for i in range(len(matrix)): for j in range(len(matrix[0])): print(f"({i}, {j}): {find_neighbors(matrix, i, j)}")