可以通过以下步骤实现:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
max_index = np.argmax(arr)
row, col = np.unravel_index(max_index, arr.shape)
neighbors = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
valid_neighbors = []
for neighbor in neighbors:
if 0 <= neighbor[0] < arr.shape[0] and 0 <= neighbor[1] < arr.shape[1]:
valid_neighbors.append(neighbor)
neighbor_values = [arr[neighbor] for neighbor in valid_neighbors]
最终,我们可以得到最大值的邻居的值列表neighbor_values。
这个方法可以应用于各种二维numpy数组,例如图像处理、矩阵操作等场景。
腾讯云相关产品和产品介绍链接地址:
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云