List
One of Python’s built-in data types is a list: list. A list is an ordered collection in which elements can be added and removed at any time.
1. Create a List
To create a list, just enclose the different data items separated by commas in square brackets, and the data in the list The items do not need to be of the same type
list1=['兩點(diǎn)水','twowter','liangdianshui',123]
2. Access the values ??in the List
Use the subscript index to access the values ??in the list. Likewise, you can You can use the form of square brackets to intercept the characters
list1=['兩點(diǎn)水','twowter','liangdianshui',123] # 通過(guò)索引來(lái)訪問(wèn)列表 print(list1[2]) # 通過(guò)方括號(hào)的形式來(lái)截取列表中的數(shù)據(jù) print(list1[0:2])
. The output result is:
3. Update List (list)
You can modify or update the data items of the list through the index, or you can use the append() method to add list items.
list1=['兩點(diǎn)水','twowter','liangdianshui',123] print(list1) # 通過(guò)索引對(duì)列表的數(shù)據(jù)項(xiàng)進(jìn)行修改或更新 list1[2]=456 print(list1) # 使用 append() 方法來(lái)添加列表項(xiàng) list1.append('hello'); print(list1)
Output results:
#4. Delete List (list)
Use del statement to delete Elements of the list
list1=['兩點(diǎn)水','twowter','liangdianshui',123] print(list1) # 使用 del 語(yǔ)句來(lái)刪除列表的的元素 del list1[3] print(list1)
Output result:
5. List operator
Operators for list pairs and * are similar to those for strings. The symbol is used for combined lists, and the * symbol is used for repeated lists.
6、List function & method
7、 Example
Finally, let’s get familiar with the operation of List through an example
Example:
#-*-coding:utf-8-*- #-----------------------list的使用---------------------------------- # 1.一個(gè)產(chǎn)品,需要列出產(chǎn)品的用戶(hù),這時(shí)候就可以使用一個(gè) list 來(lái)表示 user=['liangdianshui','twowater','兩點(diǎn)水'] print('1.產(chǎn)品用戶(hù)') print(user) # 2.如果需要統(tǒng)計(jì)有多少個(gè)用戶(hù),這時(shí)候 len() 函數(shù)可以獲的 list 里元素的個(gè)數(shù) len(user) print('\n2.統(tǒng)計(jì)有多少個(gè)用戶(hù)') print(len(user)) # 3.此時(shí),如果需要知道具體的用戶(hù)呢?可以用過(guò)索引來(lái)訪問(wèn) list 中每一個(gè)位置的元素,索引是0從開(kāi)始的 print('\n3.查看具體的用戶(hù)') print(user[0]+','+user[1]+','+user[2]) # 4.突然來(lái)了一個(gè)新的用戶(hù),這時(shí)我們需要在原有的 list 末尾加一個(gè)用戶(hù) user.append('茵茵') print('\n4.在末尾添加新用戶(hù)') print(user) # 5.又新增了一個(gè)用戶(hù),可是這個(gè)用戶(hù)是 VIP 級(jí)別的學(xué)生,需要放在第一位,可以通過(guò) insert 方法插入到指定的位置 # 注意:插入數(shù)據(jù)的時(shí)候注意是否越界,索引不能超過(guò) len(user)-1 user.insert(0,'VIP用戶(hù)') print('\n5.指定位置添加用戶(hù)') print(user) # 6.突然發(fā)現(xiàn)之前弄錯(cuò)了,“茵茵”就是'VIP用戶(hù)',因此,需要?jiǎng)h除“茵茵”;pop() 刪除 list 末尾的元素 user.pop() print('\n6.刪除末尾用戶(hù)') print(user) # 7.過(guò)了一段時(shí)間,用戶(hù)“l(fā)iangdianshui”不玩這個(gè)產(chǎn)品,刪除了賬號(hào) # 因此需要要?jiǎng)h除指定位置的元素,用pop(i)方法,其中i是索引位置 user.pop(1) print('\n7.刪除指定位置的list元素') print(user) # 8.用戶(hù)“兩點(diǎn)水”想修改自己的昵稱(chēng)了 user[2]='三點(diǎn)水' print('\n8.把某個(gè)元素替換成別的元素') print(user) # 9.單單保存用戶(hù)昵稱(chēng)好像不夠好,最好把賬號(hào)也放進(jìn)去 # 這里賬號(hào)是整數(shù)類(lèi)型,跟昵稱(chēng)的字符串類(lèi)型不同,不過(guò) list 里面的元素的數(shù)據(jù)類(lèi)型是可以不同的 # 而且 list 元素也可以是另一個(gè) list newUser=[['VIP用戶(hù)',11111],['twowater',22222],['三點(diǎn)水',33333]] print('\n9.不同元素類(lèi)型的list數(shù)據(jù)') print(newUser)