Using np.argwhere
and itertools.product
:
import numpy as np
from itertools import product
def corners(np_array):
ind = np.argwhere(np_array)
res = []
for f1, f2 in product([min,max], repeat=2):
res.append(f1(ind[ind[:, 0] == f2(ind[:, 0])], key=lambda x:x[1]))
return res
corners(arr)
Output:
[array([1, 1], dtype=int64),
array([2, 1], dtype=int64),
array([1, 3], dtype=int64),
array([2, 2], dtype=int64)]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…