简体   繁体   English

在Python中使用切片[:0:-1]反转列表

[英]reversing list using slicing [ : 0:-1] in Python

I am confused in the list slicing in Python. 我在Python的列表切片中感到困惑。

For a list 列表

L=[0,1,2,3,4,5,6,7,8,9,10]

I wanted to reverse the list and could get the answer by 我想颠倒这个清单,然后就可以得到答案了

L[::-1] 

getting [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] . 获得[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

However, when I tried 但是,当我尝试

L[10:0:-1]

I got [10,9,8,7,6,5,4,3,2,1] without 0 . 我得到[10,9,8,7,6,5,4,3,2,1]没有0 Neither L[10:1:-1] nor L[10:-1:-1] give the answer. L[10:1:-1]L[10:-1:-1]给出答案。

On the other hand, L[200::-1] , L[10:-12:-1] gives correct answer, though L[200] , L[-12] is out of bounds. 另一方面, L[200::-1]L[10:-12:-1]给出正确的答案,尽管L[200]L[-12]超出界限。

I would like to understand the underlying logic of Python for this case. 我想了解这种情况下Python的基本逻辑。 Thank you. 谢谢。

Let's take a list for example, 我们以列表为例,

a = [1, 2, 3, 4, 4, 5, 6, 9]

If you try to slice it using positive indices, 如果您尝试使用正指数对其进行切片,

newa = a[1:5] 

This will result in 这将导致

newa = [2, 3, 4, 4] 

This is because, in the above case slicing occurs like this, a[inclusive : exclusive], first index is included and slicing begins from this index, and end just one index before the index(5), exclusive(remember). 这是因为,在上述情况下,切片发生如下,包括[包含:异或],第一个索引,切片从该索引开始,并在索引(5)之前只结束一个索引,独占(记住)。 This is just the way how list slicing works. 这就是列表切片的工作方式。

To get last two values of the list a, 要获取列表a的最后两个值,

newa = [6 : ]

Here, the index starts from 6(inclusive) and ends till end of the list. 这里,索引从6(包括)开始,一直到列表的结尾。

If you are keen in using negative indexing in lists as Python offers to ease up indexing, know once and for all the inclusive and exclusive thing about slicing. 如果您热衷于在列表中使用负索引作为Python提供的简化索引,请一劳永逸地了解切片的包容性和排他性。

For same example above, to get last two numbers of the list using negative index, what we do is, 对于上面的相同示例,要使用负索引获取列表的最后两个数字,我们所做的是,

newa = a[-2 : ]

In the case of positive indexing, the index begins at 0 while for negative indexing, the index is at -1, -2 and so on. 在正索引的情况下,索引从0开始,而对于负索引,索引在-1,-2,依此类推。 So in the above example, starting from second last number in the list, slice till the end of the list, is what is understood. 因此,在上面的示例中,从列表中的倒数第二个开始,切片直到列表的末尾,是可以理解的。

Now, to reverse the list, one could do, 现在,为了扭转名单,人们可以这样做,

print a.reverse()
[9, 6, 5, 4, 4, 3, 2, 1]

In slicing way, list can be reversed by giving it a [start, end, step] like mentioned above, but I would like to clarify it further. 在切片方式中,列表可以通过给出如上所述的[开始,结束,步骤]来反转,但我想进一步澄清它。

 r = a[2: : -1]

This will make a new list starting with number from index 2, and till the end of the list, but since the step is -1, we decrease from index 2, till we reach 0. Hence we come up with a reversed list of first three numbers as below: 这将创建一个新的列表,从索引2的数字开始,直到列表的结尾,但由于步骤为-1,我们从索引2减少,直到我们达到0.因此我们得出一个反向列表的第一个三个数字如下:

r = [3, 2, 1]

Also, to get a whole of the list as reversed, 另外,要将整个列表视为反转,

r = a[len(a): : -1]

This will start from 8, the length of the list, but in indices terms which begins from 0, we have 0 till 8 indices, so starting from 8th indices, it goes on decreasing in steps of -1, till the end of the list. 这将从列表的长度8开始,但是从0开始的索引术语中,我们有0到8个索引,因此从第8个索引开始,它逐步减少-1,直到列表的末尾。 The result is as: 结果如下:

[9, 6, 5, 4, 4, 3, 2, 1]

I personally prefer to use this for reversing the list. 我个人更喜欢使用它来反转列表。

For the Python list slicing syntax, list[start:end:step] will get a sliced list with items starting with list[start] , but list[end] is excluded. 对于Python列表切片语法, list[start:end:step]将获得一个切片列表,其中的项目以list[start] ,但list[end]被排除在外。 As a result, in your case, L[10:0:-1] will exclude L[0] , ie , 0, and L[10::-1] will work as you expect. 因此,在您的情况下, L[10:0:-1]将排除L[0] 0,并且L[10::-1]将按预期工作。

When the start or end is a negative number, it means it counts from the end of the list. startend是负数时,表示它从列表末尾开始计数。 So list[-1] will get the last item in the list. 所以list[-1]将获得列表中的最后一项。 In your case, L[10:-1:-1] is equivalent to L[10:10:-1] . 在你的情况下, L[10:-1:-1]相当于L[10:10:-1] So L[10:-1:-1] will get [] . 所以L[10:-1:-1]会得到[]

 >>> L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 >>> L[1:3]
 [1, 2]

index 3 is excluding 指数3不包括在内

>>> L[0:10:1]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

index 10 is excluding, and if you want to select all,you should use: 索引10是排除的,如果要选择全部,则应使用:

>>>L[0:11:1]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

but you can not get L[11] ,it will be throw IndexError ,because you only have 11 elements,the max index is 10, the reason L[0:11:1] will not out of bound because this slice will not access to L[11] only from index 0 to 10. 但你不能得到L[11] ,它会抛出IndexError ,因为你只有11个元素,最大索引是10,原因L[0:11:1]不会超出界限,因为这个切片不会访问到L[11]仅从索引0到10。

>>> L[10:0:-1]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

-1 is a step of slice reversely,also 0 is excluding, -1是反向切片的步骤,也排除0

and L[10:-1:-1] is equivalent to L[10:10:-1] ,because the first -1 means the last index of L 并且L[10:-1:-1]相当于L[10:10:-1] ,因为第一个-1表示L的最后一个索引

>>> L[10:-12:-1]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

index -11 is equivalent to index 0 , index -12 is equivalent to index before 0 index -11相当于索引0 ,index -12相当于0之前的索引

The above answers correctly explain what happens when list slices are used. 以上答案正确解释了使用列表切片时会发生什么。 However, I would like to touch upon the final part of the question - how does L[200::-1] or L[10:-12:-1] work even though both 200 and 12 are out of bounds of the array? 但是,我想谈谈问题的最后部分 - 即使200和12都超出数组范围, L[200::-1]L[10:-12:-1]工作? ?

You can simply understand it this way - When slices are used to extract elements from a sequence, python ensures that the indices used within the slice are within the the length of the sequence. 您可以通过这种方式简单地理解它 - 当切片用于从序列中提取元素时,python确保切片中使用的索引在序列的长度内。 Hence in your case, when you specify L[200::-1] , what happens internally is that Python checks that the length of the sequence L is only 11 and hence treats the above statement as L[10::-1] instead. 因此在你的情况下,当你指定L[200::-1] ,内部发生的事情是Python检查序列L的长度仅为11 ,因此将上述语句视为L[10::-1] Similar argument applies to L[10:-12:-1] . 类似的论点适用于L[10:-12:-1]

This link provides a good insight into section-slices. 链接提供了对切片的良好洞察。 Typically at the end of this page there is a description and an example on how to implement sequences that support extended slicing. 通常在本页末尾有一个描述和一个关于如何实现支持扩展切片的序列的示例。 To quote: 报价:

slice objects now have a method indices(length) which, given the length of a sequence, returns a (start, stop, step) tuple that can be passed directly to range(). 切片对象现在有一个方法索引(长度) ,给定序列的长度,返回一个(开始,停止,步骤)元组,可以直接传递给range()。 indices() handles omitted and out-of-bounds indices in a manner consistent with regular slices (and this innocuous phrase hides a welter of confusing details!). indices()以与常规切片一致的方式处理省略和越界索引(这个无害的短语隐藏了一系列令人困惑的细节!)。

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

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