In converting a nice simple loop to a more pythonic version, I introduced a subtle (well, fine, subtle is in the eye of the beholder) bug.
The data structure is three 1D arrays:
sums[10]of floatsindices[2]of integers in [0,10); i.e., indices into sum[]how_much[2]of floats
Original code:
for i in range(2):
sums[indices[i]] += how_much[i]
So if sums starts all zeros, and indices=[3,4] and how_much=[.1,.2], then we will set sums[3]=.1 and sums[4]=.2.
Pythonic version:
sums[indices] += how_much
All fine -- except for the corner case when indices[0] == indices[1]. So if sums again starts all zeros, indices=[3,3] and how_much=[.1, .2]... then
the first loop sets sums[3]=.3, while the Pythonic "equivalent" sets sums[3]=.2.
I've not found this case specifically addressed in the documentation, but I'll concede that numpy is operating reasonably. Unfortunately, that behavior is incorrect for my application.
Can anyone think of a nice Pythonic way to implement the loop? (Of course, in real life, the arrays are much larger, and I execute the loop many times).
whereis a reserved keyword in NumPy.whereis a function in NumPy.sumis also a function, not a keyword.