[Numpy-discussion] Advanced selection, duplicate indices, and augmented assignment

Robert Kern robert.kern at gmail.com
Sat Jan 6 18:34:59 EST 2007

Sean R. Lynch wrote:

>>>> x = zeros((3,))
>>>> x[array([0, 1, 1])] += array([1, 1, 1])
>>>> x
> array([ 1.,  1.,  0.])
> If this worked the way I was hoping, the output would be [1 2 0] because
> it would add to element 1 twice due to its duplication in the advanced
> selection array.
> Is the current behavior intentional or is it an accident of
> implementation?

It is an unavoidable consequence of the way Python interprets that code and the
way numpy arrays are fundamentally implemented. See Travis Oliphant's, Tim
Hochberg's and my posts in the thread "Histograms via indirect index arrays" for
more details.


Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

More information about the NumPy-Discussion mailing list