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

前端 未结 7 1094
深忆病人
深忆病人 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].

    0 讨论(0)
提交回复
热议问题