在 numpy 文档中关于 basic slicing ,有一种情况描述为:
basic slicing is also initiated if the selection object is any non-ndarray sequence (such as a list) containing slice objects, the Ellipsis object, or the newaxis object, but not for integer arrays or other embedded sequences.
很难理解这个描述。这种情况发生在什么时候?你能给我举一些这种情况的例子吗?
最佳答案
这是一个有趣的便利。假设我有以下数组:
arr = np.arange(100)
如果我是傻的,我可以像这样对数组进行切片:
arr[:30, ..., np.newaxis]
(省略号
在这里没有任何帮助,但我可以 把它放在那里)。基本上,这告诉基本切片采用前 30 个元素,然后添加一个新轴。
如果我愿意(或者无论出于何种原因更方便),我可以像这样完成相同的切片:
arr[[slice(0, 30), Ellpisis, np.newaxis]]
如果我有一个二维数组:
arr = arr.reshape((10, 10))
我可以有多个切片对象:
arr[[slice(0, 5), slice(5, 10, 2)]]
所以我想一般来说,您可以认为 arr[[x_0, x_1, x_2, ..., x_N]]
等同于 arr[x_0, x_1, x_2 , ..., x_N]
只要 x_0
, ... x_N
都是切片、省略号或 None。
关于python - 需要使用 `non-ndarray sequence` 进行 numpy 基本索引的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38130054/