Reverse Indexing in Python?

2020-08-12 18:23发布

I know that a[end:start:-1] slices a list in a reverse order.

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.

print a[15:-1:-1] # prints []  

Any ideas?

标签: python
6条回答
乱世女痞
2楼-- · 2020-08-12 18:36

In Python2.x, the simplest solution in terms of number of characters should probably be :

>>> 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.

>>> 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.

>>> 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

查看更多
放我归山
3楼-- · 2020-08-12 18:45

You can assign your variable to None:

>>> a = range(20)
>>> a[15:None:-1]
[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> 
查看更多
对你真心纯属浪费
4楼-- · 2020-08-12 18:51

Omit the end index:

print a[15::-1]
查看更多
beautiful°
5楼-- · 2020-08-12 18:58
>>> 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].

As you can see in here as well:

>>> a[0:2:]
[0, 1]

-1 is the last value in a:

>>> a[len(a)-1] == a[-1]
True
查看更多
来,给爷笑一个
6楼-- · 2020-08-12 18:59

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]
查看更多
Juvenile、少年°
7楼-- · 2020-08-12 19:01

EDIT: begin and end are variables

I never realized this, but a (slightly hacky) solution would be:

>>> a = range(5)
>>> s = 0
>>> e = 3
>>> b = a[s:e]
>>> b.reverse()
>>> print b
[2, 1, 0]
查看更多
登录 后发表回答