Using pure numpy , and avoiding the for loop:
np.add.at(a, np.array([1,2,2,1,3]), np.array([1,1,1,1,1]))
Output:
>>> a = np.array([0,0,0,0,0,0]) >>> np.add.at(a, np.array([1,2,2,1,3]), np.array([1,1,1,1,1])) >>> a array([0, 2, 2, 1, 0, 0])
Please note that this is a replacement in place. This is what you want, but it may not be desirable for future viewers. Hence the note :)
source share