append 向后追加
1 name_list=["eirc","alex","tony"] 2 name_list.append('seven') 3 print(name_list) 4 #输出结果['eirc', 'alex', 'tony', 'seven']
count 计算元素出现次数
1 name_list=["eirc","alex","tony"] 2 name_list.append('seven') 3 name_list.append('seven') 4 name_list.append('seven') 5 name_list.append('seven') 6 print(name_list.count('seven')) 7 8 #输出结果 4
extent 扩展(批量添加)
# iterable 可迭代的
1 name_list=["eirc","alex","tony"] 2 name_list.append('seven') 3 name_list.append('seven') 4 name_list.append('seven') 5 name_list.append('seven') 6 print(name_list.count('seven')) 7 temp=[111,222,33,44] 8 name_list.extend(temp) 9 print(name_list)
#输出结果
4
['eirc', 'alex', 'tony', 'seven', 'seven', 'seven', 'seven', 111, 222, 33, 44]
index 获取指定元素的索引
1 name_list=["eirc","alex","tony"] 2 name_list.append('seven') 3 print(name_list.index("alex")) 4 #输出结果 1
insert 向指定索引插入元素
1 name_list=["eirc","alex","tony"] 2 name_list.insert(1,'SEVEN') 3 #在索引值为1的位置插入SEVEN 4 print(name_list) 5 #输出结果['eirc', 'SEVEN', 'alex', 'tony']
pop 在原list中移除列表尾部,并且可以将其赋值给其他变量
1 name_list=["eirc","alex","tony"] 2 i = name_list.pop() 3 print(i) 4 print(name_list) 5 #输出结果tony 6 ['eirc', 'alex']
remove 在list中移除某个元素(从左边找到的第一个元素)
1 name_list=["eirc","alex","tony"] 2 name_list.remove("tony") 3 print(name_list) 4 #输出结果 5 ['eirc', 'alex']
reverse 将list中的元素翻转
1 name_list=["eirc","alex","tony"] 2 name_list.reverse() 3 print(name_list) 4 #输出结果['tony', 'alex', 'eirc']
sort 排序
1 name_list=["eirc","alex","tony"] 2 name_list.sort() 3 print(name_list) 4 #输出结果['alex', 'eirc', 'tony']
del 删除指定位置的元素(也可以执行切片删除)
1 name_list=["eirc","alex","tony"] 2 del name_list[1] 3 print(name_list) 4 #输出结果['eirc', 'tony']
1 name_list=["eirc","alex","tony","seven"] 2 del name_list[1:3] 3 print(name_list) 4 #输出结果['eirc', 'seven']