Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的切片列表或字符串对象_Python_Python 3.x_String_List_Python 2.7 - Fatal编程技术网

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。您决定询问社区,这很好,但是这些信息您可以在官方文档中轻松找到。那么,重点是什么?