为什么slice [:-0]在Python中返回空列表 [英] Why does slice [:-0] return empty list in Python
本文介绍了为什么slice [:-0]在Python中返回空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
今天在编写一些单元测试时偶然发现了一些令人困惑的东西:
Stumbled upon something slightly perplexing today while writing some unittests:
blah = ['a', 'b', 'c']
blah[:-3] # []
blah[:-2] # ['a']
blah[:-1] # ['a', 'b']
blah[:-0] # []
我一辈子都无法弄清楚为什么blah[:-0] # []
应该是这种情况,该模式似乎似乎暗示它应该是['a', 'b', 'c']
.任何人都可以帮助阐明为什么会这样吗?未能在文档中找到关于为什么会出现这种情况的提示.
Can't for the life of me figure out why blah[:-0] # []
should be the case, the pattern definitely seems to suggest that it should be ['a', 'b', 'c']
. Can anybody help to shed some light on why that is the case? Haven't been able to find mention in the docs as to why that is the case.
推荐答案
-0
是0
,并且从list
包含开头的索引到索引0
不包含在内的切片是空的list
.
-0
is 0
, and a slice that goes from the beginning of a list
inclusive to index 0
non-inclusive is an empty list
.
这篇关于为什么slice [:-0]在Python中返回空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文