I think you could use bincount , at least for the 1-D case:
>>> A = np.array([1,3,3,3,29]) >>> B = np.array([2,3,4,2,3]) >>> np.bincount(A, B) array([ 0., 2., 0., 9., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 3.])
(In addition, duplicate values ββwhen using numpy indexing can behave very strange, and itβs very easy to get the behavior that you see in simple cases out of the way. I completely avoid them, since the behavior is almost never what I want.)