简体   繁体   English

以numpy反向索引索引0

[英]Indexing index 0 in reverse order in numpy

I have created a matrix a, and I'm trying to index it in reverse order. 我创建了一个矩阵a,并且尝试以相反的顺序对其进行索引。 These are my outputs: 这些是我的输出:

>>> a

array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23],
       [24, 25, 26, 27],
       [28, 29, 30, 31],
       [32, 33, 34, 35],
       [36, 37, 38, 39],
       [40, 41, 42, 43],
       [44, 45, 46, 47]])

>>> print a[2:0:-1]
[[ 8  9 10 11]
 [ 4  5  6  7]]

As you can see I'm not getting the first row. 如您所见,我没有获得第一行。 Now, when I try this, I get an empty arrow output, as it treats it as 2 .. n - 1. 现在,当我尝试此操作时,我得到一个空箭头输出,因为它将其视为2 .. n-1。

>>> print a[2:-1:-1]
[]

I'm wondering is there anyway to achieve a reverse iteration including the 0th index using indexing notation in numpy? 我想知道是否有实现反向迭代,包括使用numpy中的索引符号表示第0个索引?

I'd like to do this in a general sense, so that as I iterate some array like so, I can make use of this: 我想在一般意义上做到这一点,这样当我像这样迭代一些数组时,我可以利用它:

>>> b = []
>>> for i in range(a.shape[0] - 3):
...     b.append(np.hstack(a[i+3:i:-1]))

Extra clarifications to show the issue: 显示问题的更多说明:

Now, if I modify this to: 现在,如果我将其修改为:

>>> b = []
>>> for i in range(a.shape[0] - 3):
...     b.append(np.hstack(a[i+2:i-1:-1]))

The above will simply output an error. 上面只会输出一个错误。 Because, in the first iteration it will be a[2:-1:-1] which will be an empty array. 因为在第一次迭代中它将是a [2:-1:-1],它将是一个空数组。

The first form simply generates the wrong output because it goes it will be interpreted as [3,0), and the second form will be interpreted as [2,n-1) which is completely wrong. 第一种形式只会产生错误的输出,因为它会被解释为[3,0),第二种形式会被解释为[2,n-1),这是完全错误的。

I'm using python 2.7, but it maybe relevant to python 3 as well. 我正在使用python 2.7,但它也可能与python 3有关。

Note that slices never include the item at the stop index [start, stop) , so the zeroth item is excluded from your slice. 请注意,切片永远不会在停止索引[start, stop)包含该项目,因此第零个项目将从切片中排除。

To slice all the way to the start, don't specify the stop index (index is passed as None ): 要从头开始切分,请不要指定停止索引(索引作为None传递):

>>> a[2::-1]
array([[ 8,  9, 10, 11],
       [ 4,  5,  6,  7],
       [ 0,  1,  2,  3]])

Specifying stop as -1 returns an empty slice since -1 connotes the last item; stop指定为-1返回一个空片,因为-1表示最后一项; one can't (in a useful sense) go from index 2 to the last item striding backwards. (在有用的意义上)不能从索引2到最后一个向后大步前进的项目。

For your use case, you can play around with expressions in the slice like short-circuiting: 对于您的用例,您可以像在短路中那样尝试切片中的表达式:

...
a[2:(i or None):-1] # 0 is falsy, so at i=0, we have None

Or use a ternary conditional to explicitly specify how you want the edge case to be handled: 或使用三元条件来显式指定要如何处理边缘情况:

...
a[2:(None if i==0 else i):-1]

Another way to do that is to use numpy.ix function which is very useful in slicing. 另一种方法是使用numpy.ix函数,该函数在切片时非常有用。

Try this: 尝试这个:

print a[np.ix_(np.arange(2,-1,-1)), :]

np.arange returns array [2, 1, 0] - sequence of rows you need. np.arange返回数组[2,1,0]-您需要的行序列。 You can pass any sequence you want inside np.ix to get rows or columns you need. 您可以在np.ix内部传递任何想要的序列,以获取所需的行或列。 See examples here . 在这里查看示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM