What does the slice() function do in Python?

后端 未结 4 1043
轻奢々
轻奢々 2020-12-02 18:52

First of all, I\'d like to clarify the question: it\'s about the slice() function, not slices of lists or strings like a[5:4:3].

The docs menti

4条回答
  •  遥遥无期
    2020-12-02 19:11

    a[x:y:z] gives the same result as a[slice(x, y, z)]. One of the advantages of a slice object is that it can be stored and retrieved later as a single object instead of storing x, y and z.

    It is often used to let the user define their own slice that can later be applied on data, without the need of dealing with many different cases.

提交回复
热议问题