程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] how to modify and copy data using lists

編輯:Python

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 .

One 、 Modifying data

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 :

  • 1、reverse Represents a collation ,reverse=True Representation of descending order ,reverse=False Expressing ascending order ( Default )
  • 2、Key: If there is dictionary data in the list at work , According to a dictionary key This is used when sorting the values of key, If you can't use it for the time being, ignore it for the time being

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]

Two 、 Copy list

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']


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved