List data modification is mainly introduced from three aspects , The first is to modify the data of the specified subscript , The second is to use the reverse order function reverse(), The third is sorting sort(). Next, copy the list data , Generally, when modifying data, you will copy a copy of the original data first and then operate . These four methods are relatively simple python Basic course , Just practice more after watching , For a deeper understanding, you can look at the official documents .
1.1 Modify the data with the specified subscript
step : This data needs to be modified first , Then re assign the data
Code quick experience :
list1 = ['python', 'java', 'php']
list1[0] = '333'
print(list1) # result :['333', 'java', 'php'] ---- The original ‘python’ The data was modified to ‘333’
1.2 The reverse reverse()
Reverse the order of the original data in the list
1、 grammar
List sequence .reverse()
2、 Code quick experience :
list2 = [1, 6, 8, 3, 7, 9]
list2.reverse()
print(list2) # result :[9, 7, 3, 8, 6, 1]
1.3 Sort sort()
Sort : Ascending ( Default ) and Descending
1、 grammar
List sequence .sort(key=None, reverse=False)
2、 Be careful :
3、 Code quick experience :
list2 = [1, 6, 8, 3, 7, 9]
# Default ascending order
list2.sort()
print(list2) # result :[1, 3, 6, 7, 8, 9]
# Descending
list2.sort(reverse=True)
print(list2) # result :[9, 8, 7, 6, 3, 1]
function : copy()
We had an original list before , If you want to copy the list data , You can use the original list name .copy(), In the working scenario, the copied data will be stored in another variable , So there will be two copies of the data , One original and one copy .
Why copy data :
Because in the working scene , Generally, they attach great importance to data ,, Because what the program controls is data , Data sources are not easy . There are several sources of data , For example, manual input 、 Crawler technology crawls the right data 、 Send questionnaires to collect data one by one 、 Input accumulated data one by one through user registration, and so on , This kind of data is very important . At work , If we want to modify or delete, we usually copy a copy , Leave the original data on the basis of other operations , No matter how you operate, the original data in the system has a reservation .
1、 grammar
List sequence name .copy()
2、 Code quick experience
list1 = ['python', 'java', 'php']
copy_list = list1.copy()
print(list1) # result :['python', 'java', 'php']
print(copy_list) # result :['python', 'java', 'php']