Python中的反向索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17610096/
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
Reverse Indexing in Python?
提问by Mohammad Moghimi
I know that a[end:start:-1]slices a list in a reverse order.
我知道a[end:start:-1]以相反的顺序对列表进行切片。
For example
例如
a = range(20)
print a[15:10:-1] # prints [15, ..., 11]
print a[15:0:-1] # prints [15, ..., 1]
but you cannot get to the first element (0 in the example). It seems that -1 is a special value.
但您无法到达第一个元素(示例中为 0)。似乎 -1 是一个特殊值。
print a[15:-1:-1] # prints []
Any ideas?
有任何想法吗?
采纳答案by zhangyangyu
You can assign your variable to None
:
您可以将变量分配给None
:
>>> a = range(20)
>>> a[15:None:-1]
[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>>
回答by Pablo
Omit the end index:
省略结束索引:
print a[15::-1]
回答by Ludo
EDIT: begin and end are variables
编辑:开始和结束是变量
I never realized this, but a (slightly hacky) solution would be:
我从来没有意识到这一点,但一个(有点hacky)解决方案是:
>>> a = range(5)
>>> s = 0
>>> e = 3
>>> b = a[s:e]
>>> b.reverse()
>>> print b
[2, 1, 0]
回答by 0x90
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> print a[:6:-1]
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7]
>>> a[7] == a[:6:-1][-1]
True
>>> a[1] == a[:0:-1][-1]
True
So as you can see when subsitute a value in start label :end: it will give you from start to end exclusively a[end].
因此,正如您所看到的,当在开始标签中替换一个值 :end: 时,它会从头到尾专门为您提供一个 [end]。
As you can see in here as well:
你也可以在这里看到:
>>> a[0:2:]
[0, 1]
-1 is the last value in a:
-1 是 a 中的最后一个值:
>>> a[len(a)-1] == a[-1]
True
回答by Paulo Almeida
If you use negative indexes you can avoid extra assignments, using only your start and end variables:
如果你使用负索引,你可以避免额外的赋值,只使用你的开始和结束变量:
a = range(20)
start = 20
for end in range(21):
a[start:-(len(a)+1-end):-1]
回答by cookie_chu
In Python2.x, the simplest solution in terms of number of characters should probably be :
在 Python2.x 中,就字符数而言,最简单的解决方案应该是:
>>> a=range(20)
>>> a[::-1]
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Though i want to point out that if using xrange(), indexing won't work because xrange() gives you an xrange object instead of a list.
虽然我想指出,如果使用 xrange(),索引将不起作用,因为 xrange() 给你一个 xrange 对象而不是一个列表。
>>> a=xrange(20)
>>> a[::-1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence index must be integer, not 'slice'
After in Python3.x, range() does what xrange() does in Python2.x but also has an improvement accepting indexing change upon the object.
在 Python3.x 之后,range() 做了 Python2.x 中 xrange() 所做的事情,但也有一个改进,可以接受对象的索引更改。
>>> a = range(20)
>>> a[::-1]
range(19, -1, -1)
>>> b=a[::-1]
>>> for i in b:
... print (i)
...
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
>>>
the difference between range() and xrange() learned from source: http://pythoncentral.io/how-to-use-pythons-xrange-and-range/by author: Joey Payne
range() 和 xrange() 的区别从来源:http: //pythoncentral.io/how-to-use-pythons-xrange-and-range/作者:Joey Payne