Normally numpy forces the left and right side of an assignment to match, so for example if I do a[:] = b
, b
must be the same shape or broadcast to the same shape as a
. But there seems to be an exception to that rule:
>>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> b = a.copy() >>> a[[0,1,2]] = b[::2] >>> a array([0, 2, 4, 3, 4, 5, 6, 7, 8, 9]) >>> a[np.arange(10)] = b[:2] >>> a array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1])
It seems to only work with 1d arrays and only if there is fancy indexing on the left side of the assignment, but I haven't been able to find documentation for this behavior anywhere. Is this behavior documented, if so where, and also can someone give an example of when it might be useful?
Update:
It seems that the numpy flatiter type behaves this way too, is there some connection between flatiter and fancy indexing that I don't know about?
>>> a.flat = [10,11] >>> a array([10, 11, 10, 11, 10, 11, 10, 11, 10, 11]) >>> a.flat[:] = [2,3,4] >>> a array([2, 3, 4, 2, 3, 4, 2, 3, 4, 2]) >>> a.flat = range(100) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])