切片操作员理解

可能重复:
python切片符号的好引用

我有点困惑,关于切片运算符在Python中做什么。 任何人都可以向我解释它是如何工作的?


切片运算符是一种从列表中获取项目以及更改它们的方法。 请参阅http://docs.python.org/tutorial/introduction.html#lists。

您可以使用它来获取列表的部分内容,跳过项目,反转列表等等:

>>> a = [1,2,3,4]
>>> a[0:2] # take items 0-2, upper bound noninclusive
[1, 2]
>>> a[0:-1] #take all but the last
[1, 2, 3]
>>> a[1:4]
[2, 3, 4]
>>> a[::-1] # reverse the list
[4, 3, 2, 1]
>>> a[::2] # skip 2
[1, 3]

第一个索引是从哪里开始,(可选)第二个索引是结束位置,第三个索引是可选步骤。

是的,这个问题是解释Python切片符号的重复。

链接地址: http://www.djcxy.com/p/26745.html

上一篇: slice operator understanding

下一篇: Extracting a range of data from a python list