def first_nonzero(self, arr, axis, invalid_val=0): mask = arr != 0 return np.where(mask.any(axis=axis), mask.argmax(axis=axis), invalid_val)
axis:0 横向查找
axis:1竖向查找
def first_nonzero(self, arr, axis, invalid_val=0): mask = arr != 0 return np.where(mask.any(axis=axis), mask.argmax(axis=axis), invalid_val)
axis:0 横向查找
axis:1竖向查找