0. definition:
A list is made up of columns of elements arranged in a specific order.
You can add anything to the list, and there can be no relationship between the elements;
Example:
demo_list = [1,"python",[0,1,2],{'URL':'https://blog.csdn.net/'}]
1. index
The first element of the list starts at 0, not 1;
Negative index, which allows the list to look up elements in reverse order.
list[0]: is the first element of the list
list[-2]: is the last element of the list
2. Modify, add and delete
Amendment:
a = [0,1,2,3,4,5,6,7,8,9] a[5] = "five" print(a) #result:[0, 1, 2, 3, 4, 'five', 6, 7, 8, 9]
Add to:
- Add at the end of the list, using list.append()
a = [0,1,2,3,4,5,6,7,8,9] a.append(10) print(a) #result:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- Add in the middle of the list, using list.insert()
a = [0,1,2,3,4,5,6,7,8,9] a.insert(5,10) print(a) #result:[0, 1, 2, 3, 4, 10, 5, 6, 7, 8, 9]
The following method is also feasible, but not commonly used:
a = [0,1,2,3,4,5,6,7,8,9] a[5:6] = [5,2018,2019,2020,2021] print(a) #result:[0, 1, 2, 3, 4, 5, 2018, 2019, 2020, 2021, 6, 7, 8, 9]
Delete:
- Delete with del
a = [0,1,2,3,4,5,6,7,8,9] del a[5] print(a) #result:[0, 1, 2, 3, 4, 6, 7, 8, 9]
- Pop up delete using pop()
a = [0,1,2,3,4,5,6,7,8,9] a.pop() print(a) #result:[0, 1, 2, 3, 4, 6, 7, 8]
- Pop anywhere element
a = [0,1,2,3,4,5,6,7,8,9] a.pop(5) print(a) #result:[0, 1, 2, 3, 4, 6, 7, 8, 9]
- Delete based on element value
week = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'] week.remove('Monday') print(week) #result:['Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
3. List sorting
- Permanent sorting
week = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'] week.sort() print(week) #result:['Friday', 'Monday', 'Saturday', 'Sunday', 'Thursday', 'Tuesday', 'Wednesday']
- Temporary sort
week = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'] print(sorted(week)) #result:['Friday', 'Monday', 'Saturday', 'Sunday', 'Thursday', 'Tuesday', 'Wednesday'] print(week) #result:['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
4. Reverse rotation
week = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'] week.reverse() print(week) #result:['Sunday', 'Saturday', 'Friday', 'Thursday', 'Wednesday', 'Tuesday', 'Monday']
Note: list.reverse() has no return value, so print(list.reverse()) and l = list.reverse() get None
The same is true for list.sort().
import numpy as np a = np.arange(10).tolist() b = a.reverse() print(b) #result:None
5. List length
week = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'] print(len(week)) #result:7
6.list.index()
Usage: l.index (value, [start, [stop]]) > integer -- return first index of value
Returns the index of the element value. You can specify the start and end of the search.
week = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'] print(week.index('Tuesday',0,5)) #result:1
The start and end points can specify the search range:
import numpy as np a = np.arange(10).tolist() b = np.arange(10).tolist() c = a + b print(c) #result:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(1) #result:1 print(c.index(1,5,15)) #result:11