Recently learning Python, feeling the slicer is a bit difficult to understand. I looked up some information on the Internet and made a summary.
To understand a slicer, you first need to know that the array starts at 0, and that the last slice is-1.
Let's first define the array a=[1,2,3,4,5]
Several formats for slices:
1. A[b:c] Understanding: Starting with the array value B (including) until the end of the array value C (not included)
Example: Print A[2:4] A of 2:4 is 3,4,5, because the last value of the slice is not included, so the output is [3,4]
2. A[b:] Understanding: Start with the index value B (including) to all subsequent values (the array has how many values are to all values)
Example: Print a[1:] A 1th of the array is, to the back all values, so the output is [2,3,4,5]
3. A[:b] Understanding: From the No. 0 value of the index to B (not included)
Example: Print A[:3] Index No. 0 is 1, 3rd is 4 (not included), so the output is [three-in-one]
4. A[b:-1] Understanding: From Start B to index last (not included)
Example: Print A[2:-1] The second of the index is 3, the last one is 5 (not included), the output is [3,4]
5. Assignment of slices
I. Assigning a new value to a slice
Example: a[0,2] = [3,4] output a result [3,4,3,4,5]
Two. Assign a value greater than the length of the slice
Example: a[1:4]=[1,2] output [1,1,2,5] can be found, the length is smaller
Slice breaks in Python