2024-05-23 14:50:30 发布
网友
Possible Duplicate:good primer for python slice notation
对于slice操作符在python中的作用,我有点困惑。有人能给我解释一下它是怎么工作的吗?
slice操作符是一种从列表中获取项目并对其进行更改的方法。见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]
第一个索引是从哪里开始,第二个索引(可选)是从哪里结束,第三个索引(可选)是步骤。
是的,这个问题是Explain Python's slice notation的重复。
slice操作符是一种从列表中获取项目并对其进行更改的方法。见http://docs.python.org/tutorial/introduction.html#lists。
您可以使用它获取列表的一部分、跳过项目、反转列表等:
第一个索引是从哪里开始,第二个索引(可选)是从哪里结束,第三个索引(可选)是步骤。
是的,这个问题是Explain Python's slice notation的重复。
相关问题 更多 >
编程相关推荐