切片运算符是一种从列表中获取项目以及更改它们的方法.请参阅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]
Run Code Online (Sandbox Code Playgroud)
第一个索引是从哪里开始,(可选)第二个索引是在哪里结束,而(可选)第三个索引是步骤.
是的,这个问题与Explain Python的切片符号重复.
| 归档时间: |
|
| 查看次数: |
27298 次 |
| 最近记录: |