为什么'abc'[ - 1]导致'c'而不是'b'?

use*_*472 0 python string

我认为它从一开始就向后计数,所以A为0,B为-1,C为-2.

Mos*_*she 7

正如亨利指出的那样,负面指数表明从右边算起.

当然,A0,B现在1C2,从左边算起:

->  ->  ->

A | B | C

0 | 1 | 2
Run Code Online (Sandbox Code Playgroud)

向后,-1是数组的结尾,如下所示:

<- <- <-
A | B| C
-3|-2|-1
Run Code Online (Sandbox Code Playgroud)

有关更多上下文,请按照这样的方式查看,就像两个副本排成一行:

A | B | C | A | B | C
-3| -2| -1| 0 | 1 | 2
Run Code Online (Sandbox Code Playgroud)

根据文档 StringList对象支持负索引,但Range对象不支持.