我是 Numpy 的新手,我正在尝试 2D 数组 Numpy, 当以两种不同的方式对数组进行切片时,我进行了以下观察
a = numpy.array([[1,2,3,4,5],[1,2,3,4,5]])
slice1 = a[:,:3]
slice1[0,0] = 100
print(a)
-- gives, 100 2 3 4 5
1 2 3 4 5
根据 numpy 文档,此行为是预期的 但考虑一下这个切片
a = numpy.array([[1,2,3,4,5],[1,2,3,4,5]])
slice2 = a[[True,True],:3]
slice2[0,0] = 100
print(a)
-- gives us
1 2 3 4 5
1 2 3 4 5
谁能解释一下这两种方法之间的区别
最佳答案
来自NumPy indexing documentation :
Advanced indexing always returns a copy of the data (contrast with basic slicing that returns a view).
毕竟,如果您想让 some_arr[[True, False, False, True, True, False]]
返回 View ,就没有办法让跨步起作用。
关于python - NumPy 2D 数组普通切片与基于 bool 的切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34457239/