First, let's start by understanding what a list is: A sequence is the most basic data structure in Python, similar to an array. Each element in the sequence is assigned a number-its position, or index (index), also called subscript the first index is 0, the second index is 1, and so on.
Create a list:
names=['xiaoming','libai','Zhangfei ','wangba']
Add at the end:
Names.append ('+')
print (names) ['xiaoming'libai ' Zhangfei ' ' Wangba ' ' - ']
Specify location insertion:
Names.insert (2,'xiaohei')
print (names) ['xiaoming'libai'Xiaohei 'zhangfei' 'wangba' []
Specify location deletion:
Names.remove ('libai') #方法1del names[0] #方法2
print (names) ['Pig''zhangfei'Wangba []
#names. Pop () #默认删除最后一个, or you can delete a specific location if you add an index
Get the index of a string:
Print (Names.index ('wangba'))2
Modify:
names[2]='Pig'
print (names) ['xiaoming'libai'Pig' 'zhangfei'wangba'250 ']
Count the number of a string:
names=['xiaoming','libai','Zhangfei ','wangba','libai',' Libai']print(Names.count ('libai') )3
Reverse list order:
Names.reverse () Print (names) ['libai'libai'Wangba 'zhangfei' 'libai' xiaoming']
Sort by ASCII code:
Names.sort () Print (names) ['libai'libai'Libai 'wangba' 'xiaoming' Zhangfei'
List Merge:
names=['xiaoming','Libai','Zhangfei','Wangba','Libai','Libai']names1=['1','2','3']names.extend (names1) ['Libai','Libai','Libai','Wangba','xiaoming','Zhangfei','1','2','3']#names1 won't go away .
Clear list:
names.clear () Print (names) []
The various functions of the Python list use the