, ndarray, - :
def max_consec_elem_ndarray(a, axis=-1):
def f(a):
return max(sum(1 for i in g) for k,g in groupby(a))
new_shape = list(a.shape)
new_shape.pop(axis)
a = a.swapaxes(axis, -1).reshape(-1, a.shape[axis])
ans = np.zeros(np.prod(a.shape[:-1]))
for i, v in enumerate(a):
ans[i] = f(v)
return ans.reshape(new_shape)
:
a = np.array([[[[1,2,3,4],
[1,3,5,4],
[4,5,6,4]],
[[1,2,4,4],
[4,5,3,4],
[4,4,6,4]]],
[[[1,2,3,4],
[1,3,5,4],
[0,5,6,4]],
[[1,2,4,4],
[4,0,3,4],
[4,4,0,4]]]])
print(max_consec_elem_ndarray(a, axis=2))
#[[[ 2. 1. 1. 3.]
# [ 2. 1. 1. 3.]]
#
# [[ 2. 1. 1. 3.]
# [ 2. 1. 1. 3.]]]