slices in python

Table of contents

1. Basic usage of slicing

2. A slice is a pseudo-independent object

 3. Slices can be used as placeholders


        As we all know, we can find a single element in a sequence type (string, list, tuple...) by indexing. Slicing is a technology that intercepts index fragments. With the help of slicing technology, we can process sequence type objects very flexibly.

        Slicing is not an exclusive operation for lists, but because lists are the most representative, the following mainly uses lists as examples.

1. Basic usage of slicing

The written form of slice: [i : i + n : m].

Among them, i is the starting index of the slice, which can be ignored when it is the first index of the list. i + n is the end position of the list, which can be ignored when it is the last position of the list. The index range is [i, i+n), m does not need to be provided, the default value is 1, and 0 is not allowed. When m is negative, the list is flipped.

Note: These values ​​can be greater than the length of the list and will not be reported as out of bounds. However, truncation will occur, that is, the search is still based on the range, but the parts beyond the range of the sequence data are all considered as null values. Finally, the null values ​​are obtained and ignored.

  • represents the entire list

  • The step size is positive and the steps are calculated from the front to the back of the list.

  •  When the step size is a negative number, the step size is calculated from the end of the list forward. Note that the minimum and maximum values ​​of the range index also need to be inverted.

  • The step size of the slice cannot be 0

  • According to the step direction (positive numbers are in forward order, negative numbers are in reverse order), when the maximum value of the index range is less than the minimum value, the returned fragment is empty.

2. A slice is a pseudo-independent object

        The return result of slicing is a new independent sequence. Taking a list as an example, what you get after slicing the list is still a new list, occupying a new memory address.

        But slicing is only a shallow copy . It copies the references in the original list, so when there is a variable-length object, the new list is subject to the original list. It is not a variable-length object. What is copied is a value. It is a variable-length list and a reference to the variable element. It shares the same space.

 3. Slices can be used as placeholders

        Slices can be taken out of the original sequence as independent objects, or they can be left in the original sequence as a placeholder.

  • As a placeholder, a slice is an empty list, which can be used to splice lists.
arr[1,2,3,4]

#x下面表示的都是一个空列表
arr[0:0]==arr[len(arr):]==len[X:X]

  •  As a placeholder, a slice is not an empty list and can replace and delete elements.

        To replace an element, first delete the elements from the starting index to the end index of the slice, and then insert the new element.

         The slice placeholder can have a step size to achieve the effect of continuous spanning or deletion. It should be noted that this usage can only support equal-length replacement.

Guess you like

Origin blog.csdn.net/weixin_57023347/article/details/131419408