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

前端 未结 7 1181
深忆病人
深忆病人 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:42

    a[0:5:-1] does not make much sense, since when you use this notation the indices mean: a[start:end:step]. When you use a negative step your end value needs to be at an "earlier" position than your start value.

提交回复
热议问题