Python(2.x)列表/子列表选择-1怪异

发布于 2024-09-13 12:32:22 字数 521 浏览 11 评论 0原文

所以我一直在玩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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

破晓 2024-09-20 12:32:22

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], in ls[0:10] there isn't ls[10].

情话难免假 2024-09-20 12:32:22

如果您想获取包含最后一个元素的子列表,请在冒号后留空:

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

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]
倾听心声的旋律 2024-09-20 12:32:22

对于这两个实例,我得到了一致的行为:

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

但请注意,列表的第十个元素位于索引 9 处,因为列表是 0 索引的。这可能就是你的困扰所在。

换句话说,[0:10] 并不是从索引 0-10 开始,它实际上是从 0 到第十个元素(这会得到索引 0-9,因为 10 不包含在内)在切片的末尾)。

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.

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

他不在意 2024-09-20 12:32:22

对我来说这似乎非常一致;正指数也不包含在内。我认为你做错了。请记住 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

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:

>>> 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
·深蓝 2024-09-20 12:32:22

当对数组进行切片时;

ls[y:x]  

获取从元素 y 到 x 的切片,但不包括 x。当您使用负索引时,它相当于使用

ls[y:-1] == ls[y:len(ls)-1]

so it,因此切片将一直到最后一个元素,但它不会包含它(根据切片)

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

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)

草莓味的萝莉 2024-09-20 12:32:22

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文