Use of functions in the python list.
First, let's first understand 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 location, or index, which is also called subscript. The first index is 0, and the second index is 1.
Create a list:
names=['XiaoMing','LiBai','ZhangFei','WangBa']
Add at the end:
names.append('250')
print(names)['XiaoMing', 'LiBai', 'ZhangFei', 'WangBa', '250']
Insert at the specified position:
names.insert(2,'XiaoHei')
print(names)['XiaoMing', 'LiBai', 'XiaoHei', 'ZhangFei', 'WangBa', '250']
Delete at the specified location:
Names. remove ('libai') # method 1del names [0] # method 2
print(names)['Pig', 'ZhangFei', 'WangBa', '250']
# Names. pop () # Delete the last one by default. If an index is added, you can also delete a specific location.
Obtain 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 strings:
names=['XiaoMing','LiBai','ZhangFei','WangBa','LiBai','LiBai']print(names.count('LiBai'))3
Reverse the 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 merging:
Names = ['xiaoming', 'liba', 'hangfei', 'hangzhou', 'liba', 'liba'] names1 = ['1', '2 ', '3'] names. extend (names1) ['libai', 'hangbaba', 'xiaoming', 'hangfei', '1', '2 ', '3'] # names1 will not disappear
Clear the list:
names.clear()print(names)[]