catalogue
Function: handle some elements of the list
How Python slicable objects are indexed
section
Function: handle some elements of the list
code:
list=[1,2,3,4,5,6,7,8,9] print(list[1:5])
Output:
[2, 3, 4, 5]
Features: left closed and right open, [1:5] represents the elements of index 1, 2, 3 and 4.
If the first parameter is not specified, start from scratch automatically.
code:
list=[1,2,3,4,5,6,7,8,9] print(list[:5])
Output:
[1, 2, 3, 4, 5]
Similarly, if the second parameter is not specified, it will automatically end at the end.
Copy list with slice
So how do you copy a list with slices?
code:
list=[1,2,3,4,5,6,7,8,9] list_1=list[2:5] print(list) print(list_1)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9] [3, 4, 5]
When we copy the list without slicing and assign values directly:
list=['cat','dog','pig'] list_1=list list.append('1') list_1.append('2') print(list) print(list_1)
Output:
['cat', 'dog', 'pig', '1', '2'] ['cat', 'dog', 'pig', '1', '2']
The original result we wanted was to add 1 to the list and add 1 to the list_ Add 2 to the list of 1, but the result is the same.
This is because this actually allows python to associate the new variable (that is, list_1) with the original list. When you use the list_1 list or the original list, you actually point to the same list.
How Python slicable objects are indexed
It includes two parts: positive index and negative index, as shown in the following figure. Take list object a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] as an example:
The following is an example to illustrate the use of negative indexes:
list=[1,2,3,4,5,6,7,8,9] print(list[:-4])
Output:
[1, 2, 3, 4, 5]
[: - 4] represents the elements with indexes of 0, 1, 2, 3 and 4, that is, from the first element to the penultimate element.
Slice parameters
Slice operation basic expression: object[start_index:end_index:step]
Step: both positive and negative numbers. The absolute value determines the "step size" when cutting data, while the positive and negative signs determine the "cutting direction". Positive indicates the value of "left to right" and negative indicates the value of "right to left". When step is omitted, it defaults to 1, that is, the value is taken in steps of 1 from left to right.
When step is positive
code:
list=[1,2,3,4,5,6,7,8,9] print(list[:6:2])
Output:
[1, 3, 5]
step=2 means that the value is taken every 2 elements from the beginning.
When step is negative
code:
list=[1,2,3,4,5,6,7,8,9] print(list[1:6:-1])
Output:
[]
The output is an empty list, indicating that no data has been obtained. This is because step=-1 determines the value from right to left, and start_index=1 to end_index=6 determines the value from left to right, which is contradictory, so no value is obtained.
Use of [a::-1]
code:
list=[1,2,3,4,5,6,7,8,9] print(list[4::-1])
Output:
[5, 4, 3, 2, 1]
When step is negative, end_index defaults to the first number, so it takes value from right to left, starting from start_ Start with index = 4 and take it all the way to "starting point" 1.
Use of [: b:-1]
code:
list=[1,2,3,4,5,6,7,8,9] print(list[:4:-1])
Output:
[9, 8, 7, 6]
When step is negative, start_index is the last number by default, so it is taken from right to left, starting from the "end" value of 9 to end_index=4 (excluding this point).
Use of [: - 1]
code:
list=[1,2,3,4,5,6,7,8,9] print(list[::-1])
Output:
[9, 8, 7, 6, 5, 4, 3, 2, 1]
When start_index and end_ If no index value is passed in and step is negative, then start_index defaults to the last number, end_index defaults to the first number, so it takes value from right to left from the last number to the first number.
Let's explain with a question:
Write a function to receive a list (including 10 integer numbers) and an integer number k, and return a new list
Function requirements:
- reverse the elements corresponding (excluding k) before the subscript k in the list;
- reverse the subscript k and subsequent elements;
code:
def fun (list,k): if k<0 or k>len(list): return 'Error' else: list1=list[k:] list2=list1[::-1] list3=list[:k] list4=list3[::-1] return list4+list2 list=[1,2,3,4,5,6,7,8,9,10] print(list) print(fun(list,3))
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [3, 2, 1, 10, 9, 8, 7, 6, 5, 4]