Numpy fancy indexing and assignment
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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