To get the last index, we can flip the order along all axes and then use np.argmax() on the matches. The idea with flipping is to make use of the efficient np.argmax that gets us the first matching index.
Thus, an implementation would be -
def last_match_index(a, value):
idx = np.array(np.unravel_index(((a==value)[::-1,::-1,::-1]).argmax(), a.shape))
return a.shape - idx - 1
Runtime test -
In [180]: a = np.random.randint(0,10,(100,100,100))
In [181]: last_match_index(a,7)
Out[181]: array([99, 99, 89])
# @waterboy5281's argwhere soln
In [182]: np.argwhere(a==7)[-1]
Out[182]: array([99, 99, 89])
In [183]: %timeit np.argwhere(a==7)[-1]
100 loops, best of 3: 4.67 ms per loop
In [184]: %timeit last_match_index(a,7)
1000 loops, best of 3: 861 µs per loop
If you are looking to get the last index along an axis, say axis=0 and iterate along two axes, let's say the last two axes, we could employ the same methodology -
a.shape[0] - (a==7)[::-1,:,:].argmax(0) - 1
Sample run -
In [158]: a = np.random.randint(4,8,(100,100,100))
...: m,n,r = a.shape
...: out = np.full((n,r),np.nan)
...: for i in range(n):
...: for j in range(r):
...: out[i,j] = np.argwhere(a[:,i,j]==7)[-1]
...:
In [159]: out1 = a.shape[0] - (a==7)[::-1,:,:].argmax(0) - 1
In [160]: np.allclose(out, out1)
Out[160]: True
7in the array?