<p>不确定这要快多少(如果有的话),但它找到了相应的单元格。你知道吗</p>
<pre><code>import numpy as np
a = np.array([[0,4,3,9,9,9],
[4,4,2,2,2,9],
[4,2,2,9,2,6],
[4,2,2,2,6,8],
[4,4,4,3,4,4]])
value = 2
idx = np.where(a == value)
idx = zip(idx[0],idx[1])
running_min = np.inf
corresponding_idx = []#corresponding cells
nb_min_list = []#location of neighbors
nb_min_idx = []
for i in idx:
nb_idx = [(i[0]+1,i[1]),(i[0]-1,i[1]),(i[0],i[1]+1),(i[0],i[1]-1)]#note no check for out of bounds.
nb_idx = [nb for nb in nb_idx if nb[0] >= 0 and nb[0] < a.shape[0] and nb[1] >= 0 and nb[1] < a.shape[1]]#test for edges
try:
nb_min = min([a[nb] for nb in nb_idx if a[nb] > value])
corresponding_idx.append(i)
nb_min_list.append(nb_min)
nb_min_idx.append([nb for nb in nb_idx if a[nb] == nb_min])
except:
pass
nb_min_loc = np.where(nb_min_list == min(nb_min_list))[0]
corresponding_cells = []
min_nbs = []
for nb in nb_min_loc:
corresponding_cells.append(corresponding_idx[nb])
min_nbs.append(nb_min_idx[nb])
print(corresponding_cells)#[(1, 2), (3, 3)]
print(min_nbs)#[[(0, 2)], [(4, 3)]]
</code></pre>