Trace each part out, this should speak for itself. Comments inlined.
In [304]: array = np.array([1, 1, 2, 3, 2, 1, 2, 3])
In [305]: np.unique(array) # unique values in `array`
Out[305]: array([1, 2, 3])
In [306]: array == 1 # retrieve a boolean mask where elements are equal to 1
Out[306]: array([ True, True, False, False, False, True, False, False])
In [307]: (array == 1).nonzero()[0] # get the `True` indices for the operation above
Out[307]: array([0, 1, 5])
In summary; the code is creating a mapping of <unique_value : all indices of unique_value in array>
-
In [308]: {i: (array == i).nonzero()[0] for i in np.unique(array)}
Out[308]: {1: array([0, 1, 5]), 2: array([2, 4, 6]), 3: array([3, 7])}
And here's the slightly more readable version -
In [313]: mapping = {}
...: for i in np.unique(array):
...: mapping[i] = np.where(array == i)[0]
...:
In [314]: mapping
Out[314]: {1: array([0, 1, 5]), 2: array([2, 4, 6]), 3: array([3, 7])}