On Thu, Mar 20, 2008 at 05:42:05PM +0000, James Philbin wrote:
> I was suprised to see this result:
> >>> import numpy as N
> >>> A = N.array([0,0,0])
> >>> A[[0,1,1,2]]+=1
> >>> A
> array([1, 1, 1])
> Is this expected? Working on the principle of least surprise I would
> expect [1,2,1] to be output.
This is a FAQ. This cannot work, because the inplace operation does not
take place as a for loop. It is a "one shot" operation, that happens "at
once". Let me rephrase this: you can think of this as a two phase
operation:
1) first you the indices of you want to modify
B = A[[0, 1, 1, 2]]
thus B = array([0, 0, 0, 0)]
2) then you add one to these:
C = B + 1 = array([1, 1, 1, 1])
3) then you assign these in the indices you are interested in:
A[[0, 1, 1, 2]] = C
Actually, there is no copy going, so B and C do not exist as temporary
arrays, but this is the idea: the operations are happening at once over
the whole array.
HTH,
Gaƫl
_______________________________________________
Numpy-discussion mailing list
[email protected]
http://projects.scipy.org/mailman/listinfo/numpy-discussion