【发布时间】:2015-08-18 03:24:34
【问题描述】:
我不确定我是否理解为什么这不起作用:
a = np.zeros((10, ))
# first slicing array
pos1 = np.zeros((10, ), dtype=np.bool)
pos1[::2] = True
a[pos1] = 1.
print a
# returns [ 1. 0. 1. 0. 1. 0. 1. 0. 1. 0.]
# second slicing array
pos2 = np.zeros((5, ), dtype=np.bool)
pos2[::2] = True
a[pos1][pos2] = 2.
print a
# still returns [ 1. 0. 1. 0. 1. 0. 1. 0. 1. 0.]
为什么第二次切片不影响整个数组?
我以为a[pos1] 只是原始数组子部分的“视图”......我错过了什么吗?
(这个例子只是一个简单的例子,没有实际用处,只是为了尝试理解,因为我经常使用这种语法,没想到会出现这个结果)
【问题讨论】:
-
可能与它是一项任务有关,......在左侧。
-
只看它,您似乎正在尝试将值分配给二维数组的子集。在分配期间,it 不得从
a[pos1]创建临时数组。您必须深入研究文档甚至源代码才能找到答案。 -
布尔索引是"advanced indexing"的一种;这与切片不同,并且不提供原始数组的视图。