Python列表/子列表选择-1怪异

时间:2021-08-23 19:04:18

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.

所以我一直在玩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

但是这并没有给我一个包含列表中最后一个元素的列表,而是一个包含UP到最后一个元素的列表。但是,如果我这样做

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的交易是什么,我意识到我不需要它。

6 个解决方案

#1


64  

In list[first:last], last is not included.

在列表[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]。

#2


12  

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]

#3


4  

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在切片的末尾不包括) 。

#4


2  

-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并不是特殊的,而是包围了两端。这样减去1意味着零减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 1 is the last 4, but since it's exclusive, we get 3.

因此,对于i = [1,2,3,4],i [2:-1]表示从第二项到开头减一(或“从左到右”),这导致[3]。第-1个元素,或元素0向后1是最后4个,但由于它是独占的,我们得到3。

I hope this is somewhat understandable.

我希望这有点可以理解。

#5


1  

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

#6


1  

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)

所以切片将到达最后一个元素,但它不会包含它(根据切片)

#1


64  

In list[first:last], last is not included.

在列表[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]。

#2


12  

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]

#3


4  

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在切片的末尾不包括) 。

#4


2  

-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并不是特殊的,而是包围了两端。这样减去1意味着零减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 1 is the last 4, but since it's exclusive, we get 3.

因此,对于i = [1,2,3,4],i [2:-1]表示从第二项到开头减一(或“从左到右”),这导致[3]。第-1个元素,或元素0向后1是最后4个,但由于它是独占的,我们得到3。

I hope this is somewhat understandable.

我希望这有点可以理解。

#5


1  

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

#6


1  

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)

所以切片将到达最后一个元素,但它不会包含它(根据切片)