为什么下面大小写中的切片运算符在 Python 中不起作用?



我正在观看python视频,根据讲师的slice操作员的逻辑,一种情况不起作用。

step value can be either +ve or -ve
-----------------------------------------
if +ve then it should be forward direction (L to R)
if -ve then it should be backward direction(R to L)
if +ve forward direction from begin to end-1
if -ve backward direction from begin to end + 1
in forward direction
-------------------------------
default : begin : 0
default : end : length of string
default step : 1
in backward direction
---------------------------------
default begin : -1
default end : -(len(string) + 1)

我尝试在python闲置上运行SATENT,并得到以下结果:

>>> x = '0123456789'
>>> x[2:-1:-1]
''
>>> x[2:0:-1]
'21'

根据规则,我应该以'210'的形式获得结果,但我得到了''

索引2:-1:-1扩展到2:9:-1。负启动或停止索引是始终扩展到len(sequence) + indexlen('0123456789') + (-1)是9。您不能以-1的步骤从2到9中获得,因此结果为空。

相反,使用2::-1一个空的(或None(停止索引表示"全部抓取"。当链球菌大小为负时,空停止索引的默认值是-len(sequence) - 1,也是-(len(sequence) + 1),以弥补停止索引是始终是独家。。

的事实。

最新更新