How to explain the reverse of a sequence by slice notation a[::-1]

前端 未结 7 1208
深忆病人
深忆病人 2020-11-27 22:12

From the python.org tutorial

Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size

7条回答
  •  不知归路
    2020-11-27 22:58

    You are confused with the behavior of the stepping. To get the same result, what you can do is:

    a[0:5][::-1]
    'olleh'
    

    Indeed, stepping wants to 'circle' around backwards in your case, but you are limiting it's movement by calling a[0:5:-1].

提交回复
热议问题