Python numpy中的反转数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15748001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reversed array in numpy?
提问by Denys S.
Numpy tentative tutorialsuggests that a[ : :-1]is a reversed a. Can someone explain me how we got there?
Numpy 暂定教程表明这a[ : :-1]是一个反向的a. 有人能解释一下我们是怎么到那里的吗?
I understand that a[:]means for each element of a(with axis=0). Next :should denote the number of elements to skip (or period) from my understanding.
我明白这a[:]意味着a(轴=0)的每个元素。接下来:应该表示根据我的理解要跳过(或句点)的元素数量。
采纳答案by askewchan
As others have noted, this is a python slicing technique, and numpy just follows suit. Hopefully this helps explain how it works:
正如其他人所指出的,这是一种 python 切片技术,而 numpy 只是效仿。希望这有助于解释它是如何工作的:
The last bit is the stepsize. The 1indicates to step by one element at a time, the -does that in reverse.
最后一位是步长。所述1一次通过一个元件指示步骤中,-确实在相反。
Blanks indicate the first and last, unless you have a negative stepsize, in which case they indicate last and first:
空格表示第一个和最后一个,除非您的步长为负,在这种情况下它们表示最后一个和第一个:
In [1]: import numpy as np
In [2]: a = np.arange(5)
In [3]: a
Out[3]: array([0, 1, 2, 3, 4])
In [4]: a[0:5:1]
Out[4]: array([0, 1, 2, 3, 4])
In [5]: a[0:5:-1]
Out[5]: array([], dtype=int64)
In [6]: a[5:0:-1]
Out[6]: array([4, 3, 2, 1])
In [7]: a[::-2]
Out[7]: array([4, 2, 0])
Line 5 gives an empty array since it tries to step backwards from the 0th element to the 5th.
The slice doesn't include the 'endpoint' (named last element) so line 6 misses 0when going backwards.
第 5 行给出了一个空数组,因为它试图从第0th 个元素向后步进到第 th 个元素5。
切片不包括“端点”(命名为最后一个元素),因此第 6 行在0向后移动时会丢失。
回答by Andrew Clark
This isn't specific to numpy, the slice a[::-1]is equivalent to slice(None, None, -1), where the first argument is the start index, the second argument is the end index, and the third argument is the step. Nonefor start or stop will have the same behavior as using the beginning or end of the sequence, and -1for step will iterate over the sequence in reverse order.
这不是特定于 numpy 的,切片a[::-1]等效于slice(None, None, -1),其中第一个参数是开始索引,第二个参数是结束索引,第三个参数是步骤。 Nonefor start 或 stop 将具有与使用序列的开头或结尾相同的行为,而-1for step 将以相反的顺序遍历序列。
回答by pradyunsg
It isn't numpy, it's Python.
它不是 numpy,它是 Python。
In Python, there are slices for sequence/iterable, which come in the following syntax
在 Python 中,有用于序列/可迭代的切片,其语法如下
seq[start:stop:step] => a slice from start to stop, stepping step each time.
All the arguments are optional, but a :has to be there for Python to recognize this as a slice.
所有参数都是可选的,但:必须有a才能让 Python 将其识别为切片。
Negative values, for step, also work to make a copy of the same sequence/iterable in reverse order:
对于 step,负值也可以以相反的顺序制作相同序列/可迭代的副本:
>>> L = range(10)
>>> L[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
And numpy follows that "rule" like any good 3rd party library..
numpy 像任何好的 3rd 方库一样遵循这个“规则”。
>>> a = numpy.array(range(10))
>>> a[::-1]
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
See this link
看这个链接

