Why does extended slicing not reverse the list?

前端 未结 2 639
没有蜡笔的小新
没有蜡笔的小新 2021-01-28 07:57

I\'m slicing lists in python and can\'t explain some results. Both of the following seem natural to me:

>>>[0,1,2,3,4,5][1:4:1]
[1, 2, 3]

>>>         


        
2条回答
  •  旧巷少年郎
    2021-01-28 08:50

    The third number in the slice is the step count. So, in [0,1,2,3,4,5][1:4:-1], the slicing starts at 1 and goes DOWN by 1 until it is less than 4, which is immediately is. Try doing this:

    >>> [0,1,2,3,4,5][4:1:-1]
    [4, 3, 2]
    

提交回复
热议问题