Why does list[::-1] not equal list[:len(list):-1]?

前端 未结 2 1037
别跟我提以往
别跟我提以往 2021-01-18 06:39

When slicing in python, omitting the end portion of the slice (ie the end in list[:end:]) results in end being define

2条回答
  •  不要未来只要你来
    2021-01-18 06:48

    The documentation you're referencing is the tutorial, which gives only an informal overview of Python syntax and semantics. It doesn't explain all the details. You'll note that the tutorial page you linked to doesn't even discuss negative indices.

    The actual documentation is given in the library reference under sequence types. Although it is a bit terse and not easy to understand on a first read, it does clarify that for a slice [i:j:k]:

    If i or j are omitted or None, they become “end” values (which end depends on the sign of k).

提交回复
热议问题