Python(2.x)列表/子列表选择-1怪异
所以我一直在玩Python并注意到一些看起来有点奇怪的事情。从列表中选择时 -1
的语义似乎不一致。
所以我有一个数字列表
ls = range(1000)
列表的最后一个元素当然是 ls[-1]
但如果我采用它的子列表,这样我就可以得到从中点到末尾的所有内容 这样做
ls[500:-1]
,但这并没有给我一个包含列表中最后一个元素的列表,而是一个包含直到最后一个元素的所有内容的列表。但是,如果我这样做,
ls[0:10]
我会得到一个还包含第十个元素的列表(因此选择器应该包含在内),那么为什么它不适用于 -1
。
我当然可以做 ls[500:]
或 ls[500:len(ls)]
(这很愚蠢)。我只是想知道 -1 的处理是什么,我意识到我在那里不需要它。
So I've been playing around with python and noticed something that seems a bit odd. The semantics of -1
in selecting from a list don't seem to be consistent.
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[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
.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在
list[first:last]
中,不包含last
。第 10 个元素是
ls[9]
,ls[0:10]
中没有ls[10]
。In
list[first:last]
,last
is not included.The 10th element is
ls[9]
, inls[0:10]
there isn'tls[10]
.如果您想获取包含最后一个元素的子列表,请在冒号后留空:
If you want to get a sub list including the last element, you leave blank after colon:
对于这两个实例,我得到了一致的行为:
但请注意,列表的第十个元素位于索引 9 处,因为列表是 0 索引的。这可能就是你的困扰所在。
换句话说,
[0:10]
并不是从索引 0-10 开始,它实际上是从 0 到第十个元素(这会得到索引 0-9,因为 10 不包含在内)在切片的末尾)。I get consistent behaviour for both instances:
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.
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).对我来说这似乎非常一致;正指数也不包含在内。我认为你做错了。请记住 range() 也是非包容性的,并且 Python 数组是从 0 索引的,下面是一个示例 Python 会话来说明:
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:
当对数组进行切片时;
获取从元素 y 到 x 的切片,但不包括 x。当您使用负索引时,它相当于使用
so it,因此切片将一直到最后一个元素,但它不会包含它(根据切片)
when slicing an array;
takes the slice from element y upto and but not including x. when you use the negative indexing it is equivalent to using
so it so the slice would be upto the last element, but it wouldn't include it (as per the slice)
-1 并不特殊,因为序列是向后读取的,它而是环绕末端。这样减一意味着零减一,互斥(并且,对于正步长值,序列是“从左到右”读取的。
所以对于
i = [1, 2, 3 , 4]
,i[2:-1]
表示从第 2 项到开始减一(或“大约到结束”),结果为[3]
。第-1个元素,或元素0向后1是最后一个
4
,但由于它是排他的,我们得到3。我希望这有点可以理解。
-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".
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 1 is the last
4
, but since it's exclusive, we get 3.I hope this is somewhat understandable.