sort_idx = np.argsort(a)
a_sorted = a[idx]
unq_first = np.concatenate(([True], a_sorted[1:] != a_sorted[:-1]))
unq_items = a_sorted[unq_first]
unq_count = np.diff(np.nonzero(unq_first)[0])
Grouping indices of unique elements in numpy