Python列表/子列表选择-1奇怪
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3451157/
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
Python list / sublist selection -1 weirdness
提问by Matti Lyra
So I've been playing around with python and noticed something that seems a bit odd. The semantics of -1in selecting from a list don't seem to be consistent.
所以我一直在玩 python 并注意到一些看起来有点奇怪的东西。-1从列表中选择的语义似乎不一致。
So I have a list of numbers
所以我有一个数字列表
ls = range(1000)
The last element of the list if of course ls[-1]but if I take a sublist of that so that I get everything from say the midpoint to the end I would do
列表的最后一个元素,如果当然,ls[-1]但如果我采用它的子列表,以便我得到从说中点到结尾的所有内容,我会做
ls[500:-1]
but this does not give me a list containing the last element in the list, but instead a list containing everything UP TO the last element. However if I do
但这并没有给我一个包含列表中最后一个元素的列表,而是一个包含直到最后一个元素的所有内容的列表。但是,如果我这样做
ls[0:10]
I get a list containing also the tenth element (so the selector ought to be inclusive), why then does it not work for -1.
我得到一个还包含第十个元素的列表(因此选择器应该包含在内),为什么它不适用于-1.
I can of course do ls[500:]or ls[500:len(ls)](which would be silly). I was just wondering what the deal with -1 was, I realise that I don't need it there.
我当然可以做ls[500:]或ls[500:len(ls)](这很愚蠢)。我只是想知道与 -1 的交易是什么,我意识到我不需要它。
采纳答案by dugres
In list[first:last], lastis not included.
在list[first:last],last不包括在内。
The 10th element is ls[9], in ls[0:10]there isn't ls[10].
第 10 个元素是ls[9],ls[0:10]没有ls[10]。
回答by eldarerathis
I get consistent behaviour for both instances:
对于这两种情况,我都得到一致的行为:
>>> ls[0:10]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> ls[10:-1]
[10, 11, 12, 13, 14, 15, 16, 17, 18]
Note, though, that tenth element of the list is at index 9, since the list is 0-indexed. That might be where your hang-up is.
但是请注意,列表的第十个元素位于索引9 处,因为列表是 0 索引的。那可能是您挂断电话的地方。
In other words, [0:10]doesn't go from index 0-10, it effectively goes from 0 to the tenth element (which gets you indexes 0-9, since the 10 is not inclusive at the end of the slice).
换句话说,[0:10]不是从索引 0-10 开始,它实际上是从 0 到第十个元素(这使您的索引为 0-9,因为切片末尾的 10 不包含在内)。
回答by Yoni Samlan
It seems pretty consistent to me; positive indices are also non-inclusive. I think you're doing it wrong. Remembering that range() is also non-inclusive, and that Python arrays are 0-indexed, here's a sample python session to illustrate:
这对我来说似乎很一致;正指数也是非包容性的。我认为你做错了。记住 range() 也是非包含的,并且 Python 数组是 0 索引的,这里有一个示例 Python 会话来说明:
>>> d = range(10)
>>> d
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> d[9]
9
>>> d[-1]
9
>>> d[0:9]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> d[0:-1]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> len(d)
10
回答by Andy
when slicing an array;
切片数组时;
ls[y:x]
takes the slice from element y upto and but not including x. when you use the negative indexing it is equivalent to using
从元素 y 到切片,但不包括 x。当您使用负索引时,它相当于使用
ls[y:-1] == ls[y:len(ls)-1]
so it so the slice would be upto the last element, but it wouldn't include it (as per the slice)
所以它所以切片将是最后一个元素,但它不会包含它(根据切片)
回答by Stefano Palazzo
-1 isn't special in the sense that the sequence is read backwards, it rather wraps around the ends. Such that minus one means zero minus one, exclusive(and, for a positive step value, the sequence is read "from left to right".
-1 在序列向后读取的意义上并不特殊,而是环绕在末端。这样减一意味着零减一,不包括(并且,对于正步长值,序列是“从左到右”读取的。
so for i = [1, 2, 3, 4], i[2:-1]means from item two to the beginning minus one (or, 'around to the end'), which results in [3].
The -1th element, or element 0 backwards 1is the last 4, but since it's exclusive, we get 3.
因此,对于i = [1, 2, 3, 4],i[2:-1]表示从第二项到开头减去一项(或“从头到尾”),结果为[3]。-1th 元素,或元素0 向后 1是最后一个4,但由于它是独占的,我们得到 3。
I hope this is somewhat understandable.
我希望这有点可以理解。
回答by mcane
If you want to get a sub list including the last element, you leave blank after colon:
如果您想获取包含最后一个元素的子列表,请在冒号后留空:
>>> ll=range(10)
>>> ll
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> ll[5:]
[5, 6, 7, 8, 9]
>>> ll[:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

