假设您有以下代码
a = np.ones(8)
pos = np.array([1, 3, 5, 3])
a[pos] # returns array([ 1., 1., 1., 1.]), where the 2nd and 4th el are the same
a[pos] +=1
最后一条指令返回
array([ 1., 2., 1., 2., 1., 2., 1., 1.])
但我希望对相同索引的分配进行求和,以便获得
array([ 1., 2., 1., 3., 1., 2., 1., 1.])
有人已经经历过同样的情况吗?