Python中的切片列表或字符串对象
切片中的默认范围是什么: 例如:Python中的切片列表或字符串对象,python,python-3.x,string,list,python-2.7,Python,Python 3.x,String,List,Python 2.7,切片中的默认范围是什么: 例如: ls = [1, 2, 3, 4, 5] ls[-3::1] #returns copy of the elements at indexes -3, -2, -1 ls[-3::-1] #returns copy of the elements at indexes -3 till the start of the list i.e ls[-5] ls[::-1] #returns copy of the reverse version of the l
ls = [1, 2, 3, 4, 5]
ls[-3::1] #returns copy of the elements at indexes -3, -2, -1
ls[-3::-1] #returns copy of the elements at indexes -3 till the start of the list i.e ls[-5]
ls[::-1] #returns copy of the reverse version of the list
ls[::1] #return the list as is
这背后的想法是什么?python是如何确定未声明的开始和结束索引的?例如[a:b:c],因此a是开始索引,b是结束索引,c表示索引之间的步长如果有一个正步长,那么默认的开始是开始,默认的结束是结束 如果您有一个负步骤,那么默认的开始是结束,默认的结束是开始 默认步骤是1。步长不能为零
我认为这涵盖了所有的可能性。如果你有一个积极的步骤,那么默认的开始就是开始,默认的结束就是结束。如果您有一个负步骤,那么默认的开始是结束,默认的结束是开始。默认步骤是1。您决定询问社区,这很好,但是这些信息您可以在官方文档中轻松找到。那么,重点是什么?