# 以list为例子,对tuple的切片类似 L = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘] L[1:3] # [‘b‘, ‘c‘] # 起始、末尾切片可省略 L[:3] # [‘a‘, ‘b‘, ‘c‘] L[1:] # [‘b‘, ‘c‘, ‘d‘, ‘e‘] # 结束位置超过list长度也可以正常输出 L[1:10] # [‘b‘, ‘c‘, ‘d‘, ‘e‘] # 负整数切片(注意逆序从-1开始,正序从0开始) L[-2:] # [‘d‘, ‘e‘] L[-2:-1] # [‘d‘] # 增加步长 L[1:4:2] # [‘b‘, ‘d‘] # 步长为负,注意起始与结束位置 L[2:3:-1] # [] L[4:2:-1] # [‘e‘, ‘d‘] # 取整个list L[:] # [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘]
# 逆序 L[::-1] # [‘e‘, ‘d‘, ‘c‘, ‘b‘, ‘a‘]
原文:https://www.cnblogs.com/babysteps/p/python_note_4.html