import numpy as np
import itertools
ind=np.array([[[1,2,3]], [[3,2,4]]])
M=4
lt=np.zeros((len(ind),1,M+1),dtype=int)
for a in range(len(ind)):
lt[a,0,list(itertools.chain(*ind[a]))]+=1
print(lt)
[[[0 1 1 1 0]]
[[0 0 1 1 1]]]
ind.shape
Out[15]: (2, 1, 3)