Contents of this section
- 0, the list of commonly used features summary
- 1. Define the list
- 2, access to the elements of the list
- 3、Slice
- 4. Additions
- 5. Insertion
- 6. Modifications
- 7. Copy
- 8. Delete
- 9. Expansion
- 10. Statistics
- 11. Flip
- 12. Sorting
- 13、Get the subscript value
- 14、Empty the list
Lists are one of the most commonly used data types in Python, with lists you can achieve the most convenient storage, modification and other operations on the data
0, the list of commonly used features summary
1. Define the list
>>> name=[]#Define the empty list >>> name=['luoahong','chenqun','wenhai','daiqiao','xiedi','guiwei']#Define a non-empty list
2, access to the elements of the list
>>> name=[] >>> name=['luoahong','chenqun','wenhai','daiqiao','xiedi','guiwei'] >>> name[0] 'luoahong' >>> name[1] 'chenqun' >>> name[2] 'wenhai' >>> name[3] 'daiqiao' >>> name[-1]# Take the last value backwards 'guiwei' >>> name[-2] 'xiedi'
Note: The subscript values of the list are taken from 0 onwards
3、Slice
>>> name=['luoahong','chenqun','wenhai','daiqiao','xiedi','guiwei'] >>> name[1:3] ['chenqun', 'wenhai'] >>> name[1:-1] ['chenqun', 'wenhai', 'daiqiao', 'xiedi'] >>> name[0:3] ['luoahong', 'chenqun', 'wenhai'] >>> name[:3] ['luoahong', 'chenqun', 'wenhai'] >>> name[3:] ['daiqiao', 'xiedi', 'guiwei'] >>> name[:2] ['luoahong', 'chenqun'] >>> name[::2] ['luoahong', 'wenhai', 'xiedi']
Slicing Summary:
① The sequence is always sliced from left to right, not from right to left.
① When slicing a list, the elements in the start position are included, the elements in the end position are excluded (also called caret), and the last position indicates the step size (names[start position:end position:step])
② If the value is taken from position 0, 0 can be omitted
③When you want to take the last value, the end bit can not be -1, because the element of the end bit is not included, so you can only leave it empty.
4. Append (append (element))
>>> name=['luoahong','chenqun','wenhai','daiqiao','xiedi','guiwei'] >>> ("liuhailin") >>> name ['luoahong', 'chenqun', 'wenhai', 'daiqiao', 'xiedi', 'guiwei', 'liuhailin']#liuhailinIt's a new addition.
5. Insert (insert (subscript value, inserted content))
>>> name=['luoahong','chenqun','wenhai','daiqiao','xiedi','guiwei'] >>> (0,"liyang")#0 indicates the subscript value to be inserted, and '1' indicates the content to be inserted >>> name ['liyang', 'luoahong', 'chenqun', 'wenhai', 'daiqiao', 'xiedi', 'guiwei'] #In the case where the subscript value is0Inserted in the place ofliyang
6. Modifications
>>> name=['luoahong','chenqun','wenhai','daiqiao','xiedi','guiwei'] >>> name[1]='chenqundage' >>> name ['luoahong', 'chenqundage', 'wenhai', 'daiqiao', 'xiedi', 'guiwei']
Note: Modifying elements in the list is straightforward names[subscript value] = new value
7. Copy (copy())
>>> name=['luoahong','chenqun','wenhai','daiqiao','xiedi','guiwei'] >>> name2=() >>> name2 ['luoahong', 'chenqun', 'wenhai', 'daiqiao', 'xiedi', 'guiwei']
Note: All the copies over here are shallow copies, only the first layer can be copied. Detailed information about shallow and deep copy:
8. Delete (del, remove(element), pop())
#Deletes elements based on subscript values
>>> name=['luoahong','chenqun','wenhai','daiqiao','xiedi','guiwei'] >>> del name[0] >>> name ['chenqun', 'wenhai', 'daiqiao', 'xiedi', 'guiwei']
# Delete by element >>> name=['luoahong','chenqun','wenhai','daiqiao','xiedi','guiwei'] >>> ('xiedi') >>> name ['luoahong', 'chenqun', 'wenhai', 'daiqiao', 'guiwei']
#Delete the last one >>> name=['luoahong','chenqun','wenhai','daiqiao','xiedi','guiwei'] >>> () 'guiwei' >>> >>>
Notes:
If pop() has a subscript value, it is deleting a specific element, and the effect is the same as that of del
>>> name=['luoahong','chenqun','wenhai','daiqiao','xiedi','guiwei'] >>> (1) #In the case of input subscript worthiness anddelThe effect is the same
'chenqun' >>> name ['luoahong', 'wenhai', 'daiqiao', 'xiedi', 'guiwei']
The del keyword removes not only elements from a list, but also variables.
>>> name=['luoahong','chenqun','wenhai','daiqiao','xiedi','guiwei']
#removingnamethis variable
del name >>> name Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'name' is not defined
Notes:
If pop() has a subscript value, it is deleting a specific element, and the effect is the same as that of del
The del keyword removes not only elements from a list, but also variables.
9、Extend(extend)
>>> name1=['luoahong','chenqun','wenhai','daiqiao','xiedi','guiwei'] >>> name2=['1','2','3','4','5','6'] >>> (name2) >>> name1 ['luoahong', 'chenqun', 'wenhai', 'daiqiao', 'xiedi', 'guiwei', '1', '2', '3', '4', '5', '6']
Note: The list of names2 is merged into names1, but the list names2 still exists, so if you want to delete the variable names2, you just need to del names2.
10. Statistics (count(element))
>>> name1=['luoahong','chenqun','wenhai','daiqiao','chenqun','xiedi','guiwei','chenqun'] >>> ('chenqun')# Count the number of elements of the element 'chenqun'. 3
11. Reverse ()
>>> name=['luoahong','chenqun','wenhai','daiqiao','xiedi','guiwei'] >>> () >>> name ['guiwei', 'xiedi', 'daiqiao', 'wenhai', 'chenqun', 'luoahong'] #Flip the whole list over
12. Sorting (sort())
>>> name=['4','3','2','1'] >>> () >>> name ['1', '2', '3', '4']
13. Get the subscript value (index (element))
>>> name=['luoahong','chenqun','wenhai','daiqiao','xiedi','guiwei'] >>> ('chenqun') 1
14. Clear the list (clear())
>>> name=['luoahong','chenqun','wenhai','daiqiao','xiedi','guiwei'] >>> () NameError: name 'names' is not defined >>> name []
For more information on Python list manipulation methods check out the related links below