Python中的扩展切片语法

2024-10-01 02:39:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我在处理一些弦,弄不懂切片是怎么工作的, 以下输出“i”

“输入”[0::-1]

鉴于以下输出“tupni”

“输入”[::-1]

我不知道的是第一个,以及负指数的切片是如何工作的


Tags: 切片指数tupni
1条回答
网友
1楼 · 发布于 2024-10-01 02:39:58

也许这些例子可以说明这一点:

In [146]: "input"[::-1] # from back to the begining. Equivalent to: "input"[len("input")::-1]
Out[146]: 'tupni'

In [147]: "input"[0::-1] # from back to the 0'th (i.e. first) element from back
Out[147]: 'i'

In [148]: "input"[1::-1] # from back to the 2ed element from back
Out[148]: 'ni'

In [151]: "input"[len("input")::-1] # from back to the last element from back (alterantive way)
Out[151]: 'tupni'

相关问题 更多 >