Add, delete, modify, query, and delete Python lists
Add, delete, modify, and query a list of Python:
A = ['Q', 'w', 'E', 'R', 't']
A is the list name, [] is the list content, ''is the list element, and 'Q' is a [0]
2. Query (also called slice ):
Print (a [0]) # retrieve the first element in the list: q
Print (a [1:]) # obtain from the second element to the end: ['w', 'E', 'R', 't']
Print (a []) # extract the element from the second to the third: ['w', 'E']
Print (a [0:-1]) # obtain the second and last elements from the first element: ['Q', 'w', 'E', 'R']
Print (a [0:-]) # extract from left to right, and obtain the second to last element: ['Q', 'w', 'E ', 'R']
Print (a [0: 2]) # separated from left to right: ['Q', 'E', 't']
Print (a [2:-1]) # extract elements from left to right from the third element to left, namely, ['E', 'w', 'q']
3. Add
Two methods are added: append and insert.
1. append adds an element to the end of the list, and only one element can be added at a time.
A. append ('y') # Add 'y' to the end of List'
Print (a) # The output result is ['Q', 'w', 'E', 'R', 't', 'y'].
B = ['A', 's', 'D',] # Insert the elements in list B to the end of list.
I = 0 #. You can use this method to add multiple elements.
For I in range (len (B) # after adding a sup to another list through append
A. append (B [I]) # Surface
Print (a) # The output result is ['Q', 'w', 'E', 'R', 't', 'y', 'A ', 'S ', 'D']
2. You can add multiple elements at a time in extend.
A. extend (['A', 's', 'D']) # Add ['A', 's', 'D'] to a list.
Print (a) # The result is ['Q', 'w', 'E', 'R', 't', 'y', 'A','s ', 'D']
3. insert adds an element to a specified position. (Only one element can be added at a time)
A. insert (2, 'z') # insert 'Z' to the end of the 3rd bits in list.
Print (a) # The result is ['Q', 'w', 'z', 'E', 'R', 't']
Iv. Change
You can directly assign a value to this element.
A [2] = 'W' # change the third element of list a to 'W'
Print (a) # The result is ['Q', 'w', 'w', 'E', 'R', 't']
A [] = '2', '3' # Replace the second and third elements of list a with '2' and '3' respectively'
Print (a) # The result is ['Q', '2', '3', 'E', 'R', 't']
5. Delete
1. remove
A. remove ('q') # Delete the 'q' element in List
Print (a) # The result is ['w', 'z', 'E', 'R', 't']
A. remove (a [1]) # Delete the [1] element in List
Print (a) # The result is ['Q', 'z', 'E', 'R', 't']
2. pop
C = a. pop (1) # Delete the [1] element in List
Print (a) # The result is ['Q', 'z', 'E', 'R', 't']
Print (c) # output the deleted Value
3. del
Del a [1] # Delete the [1] element in List
Print (a) # The result is ['Q', 'z', 'E', 'R', 't']
6. Number of Computing element occurrences
D = ['2', '1', '3', '3', '3', '2'] # define List d
E = d. count ('2') # calculate the number of times the element '2 appears in the d list'
Print (e) # The output computation result is 2.
7. Locate the Location Based on the Content
The Index can only be used to locate the first occurrence of an element. If this element appears only once, the position to be searched is the entire position in the list. If this element appears multiple times in the list, it must be processed together with slices. The idea of common processing: first locate the first occurrence, then search again from the first occurrence location to the last element, and then locate all the positions of the element in sequence.
F = d. index ('1') # Find that the element '1' in the d list is the first element in the list.
Print (f) # output query value
8. Sort values in the list in descending order.
Reverse can re-arrange all elements in the list in Reverse order.
T = ['1','2','3','4','5'] # Define list t
T. reverse () # Sort all the elements in list t in reverse order
Print (t) # output list t
2. Sorting from small to large
Sort () can be used to rearrange numbers by size, or Sort uppercase letters in the order of 26 English letters before lowercase. Strings are listed in the first letter. (In essence, it is sorted in ascending order by ASCII code, that is, numbers <upper-case letters <lower-case letters)
J = [3, 2, 4, 1, 0] # define list j
J. sort () # sort the elements in list j from small to large
Print (j) # output the redefined list j: [, 4]
H = ['s', 'D', 'h', 'A', 'k', 'k', 'A', 'D'] # define list h
H. sort () # rearrange the list h
Print (h) # output the redefined list h is ['A', 'D', 'k', 'A', 'D', 'h', 'k ', 'S ']
N = ['s', 'D', 'C', '5', '4', 'D'] # define list n
N. sort () # rearrange the list n
Print (n) # The result is ['4', '5', 'D', 'C', 'D', 's'].