The numpy_indexed package has an efficient and general solution to this:
import numpy_indexed as npi
result = npi.indices(A, B)
Note that it has a kwarg to set a mode for dealing with missing values; and it works with nd-arrays of any type just the same, as it does with 1d integer arrays.