方法名 | 用法 | 结果 | 说明 |
|---|
append() | list.append('d') | list=['a', 'b', 'c','d'] | 在列表末尾添加新的对象 |
del 语句 | del list[2] | list=['a', 'b', 'd'] | 删除列表中的元素 |
remove() | list.remove('a') | list=['b', 'd'] | 移除列表中某个值的第一个匹配项 |
count() | list.count('b') | 1 | 统计某个元素在列表中出现的次数 |
extend() | seq = ('a', 'b', 'c') list = list(seq) list2 = ['d','e','a'] list.extend(list2) list3 = list print (list3) | ['a', 'b', 'c', 'd', 'e','a'] 踩坑点:直接使用 list3 = list.extend(list2) 输出list3会显示None 因为extend()是原地修改方法,没有返回值,所以会None | 在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表) |
index() | list3.index('a') | 0 | 从列表中找出某个值第一个匹配项的索引位置 |
insert() | list3.insert(0,'f') | ['f', 'a', 'b', 'c', 'd', 'e', 'a'] | 将对象插入列表 |
pop() | list3.pop() list3.pop(1) | ['f', 'a', 'b', 'c', 'd', 'e''] ['f', 'b', 'c', 'd', 'e', 'a'] | 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值 |
reverse() | list3.reverse() | ['e', 'd', 'c', 'b', 'a', 'f'] 踩坑点:print(list3.reverse())返回None 执行list3.reverse()再输出 print (list3)就正常 | 反向列表中元素 |
sort() | list3.sort() | ['a', 'b', 'c', 'd', 'e', 'f'] | 对原列表进行排序 |
copy() | list6 = list3.copy() | ['a', 'b', 'c', 'd', 'e', 'f'] | 复制列表 |
clear() | list6.clear() | [] | 清空列表 |