Python列表切片操作

敲程式碼的小風發表於2020-10-31
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> lis = [0,1,2,3,4,5,6,7,8,9]
>>> lis
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> lis[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> lis[9:0:-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> lis[-1:-10:-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> lis[9:-10:-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> lis[-1:0:-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> 
>>> 
>>> 
>>> lis[-1:-10:1]
[]
>>> lis[-1:0:-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> lis[-1:-0:-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> lis[::1]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> lis[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> lis[::]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> lis[::-2]
[9, 7, 5, 3, 1]
>>> lis[3:6]
[3, 4, 5]
>>> lis[3:-2]
[3, 4, 5, 6, 7]
>>> 

相關文章