我需要使用负动态索引([: - index])对列表进行切片.这很容易,直到我意识到如果我的动态索引的值为0,则没有返回任何项目,而是返回整个列表.我怎样才能以索引为0的方式实现它,它返回整个字符串?我的代码很长很复杂,但基本上这个例子显示了问题:
arr='test text'
index=2
print arr[:-index]
>>'test te' #Entire string minus 2 from the right
index=1
print arr[:-index]
>>'test tex' #Entire string minus 1 from the right
index=0
print arr[:-index]
>>'' #I would like entire string minus 0 from the right
Run Code Online (Sandbox Code Playgroud)
注意:我使用的是Python 2.7.