列表:
names = [‘a‘,‘b‘,‘c‘,‘d‘]
1、追加:names.append()
>>> names.append(‘e‘) >>> names [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘]
2、删除:pop,remove,del
1)pop()
>>> names.pop() ‘e‘
如果没有指定下标,则默认会删除最后一个元素
>>> names.pop(2) ‘c‘
指定下标时,就会删除下标所对应的元素
2)remove()
>>> names.remove(‘e‘) >>> names [‘a‘, ‘b‘, ‘c‘, ‘d‘]
3)del
>>> del names[4] >>> names [‘a‘, ‘b‘, ‘c‘, ‘d‘]
3、查找元素所在位置:index()
>>> names.index(‘c‘) 2
4、统计元素的次数:count()
>>> names.append(‘d‘) >>> names.count(‘d‘) 2
5、反转:reverse()
>>> names.reverse() >>> names [‘d‘, ‘c‘, ‘b‘, ‘a‘]
6、清空:clear()
>>> names.clear() >>> names []
7、插入:insert()
>>> names.insert(2,‘devilf‘) >>> names [‘a‘, ‘b‘, ‘devilf‘, ‘c‘, ‘d‘]
还有其他的插入方法:
>>> names[3] = ‘lebron‘ >>> names [‘a‘, ‘b‘, ‘devilf‘, ‘lebron‘, ‘d‘]
8、排序:sort()按照ascii码来进行排序
>>> names.insert(4,‘&&‘) >>> names [‘a‘, ‘b‘, ‘d‘, ‘devilf‘, ‘&&‘, ‘lebron‘] >>> names.sort() >>> names [‘&&‘, ‘a‘, ‘b‘, ‘d‘, ‘devilf‘, ‘lebron‘]
9、拼接两个列表:extend()
>>> names.extend(place) >>> names [‘&&‘, ‘a‘, ‘b‘, ‘d‘, ‘devilf‘, ‘lebron‘, ‘beijing‘, ‘shandong‘, ‘usa‘]
10、对列表进行切片处理
1)列出所有的元素
>>> names[::] [‘&&‘, ‘a‘, ‘b‘, ‘d‘, ‘devilf‘, ‘lebron‘, ‘beijing‘, ‘shandong‘, ‘usa‘]
2)列出最后一个元素,从中间位置开始,列出后面所有的元素
>>> names[-1] ‘usa‘
>>> a = int(len(names)/2) >>> names[a:] [‘devilf‘, ‘lebron‘, ‘beijing‘, ‘shandong‘, ‘usa‘]
11、复制:copy()
>>> names.copy() [‘&&‘, ‘a‘, ‘b‘, ‘d‘, ‘devilf‘, ‘lebron‘, ‘beijing‘, ‘shandong‘, ‘usa‘]