Python中的扩展切片语法

时间:2014-12-01 04:14:27

标签: python

我正在使用一些字符串和coudn来让自己了解切片是如何工作的, 以下输出" i"

"输入" [0 :: - 1]

而以下输出" tupni"

"输入" [:: - 1]

我不能得到的是第一个,以及负指数的切片是如何工作的

1 个答案:

答案 0 :(得分:3)

也许这些例子可以澄清这一点:

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'