1.创建列表
>>> list1 = ['anmi','kan','seven']
2.查看列表
>>> list1 ['anmi', 'kan', 'seven'] >>> list1[0] 'anmi' >>> list1[1] 'kan' >>> list1[2] 'seven'
3.追加元素
>>> list1.append('selina') >>> list1.append('kan') #元素值可以重复 >>> list1
4.索引
>>> list1 ['anmi', 'kan', 'seven', 'selina', 'kan'] >>> list1.index('kan') 1 #从左开始查找到第一个匹配的后,就输出
5.统计
>>> list1.count('kan') 2 >>>
6.插入
>>> list1.insert(2,'eric') >>> list1 ['anmi', 'kan', 'eric', 'seven', 'selina', 'kan']
7.删除最后一个元素
>>> list1.pop() 'kan'
8.删除指定的元素
>>> list1 ['anmi', 'kan', 'eric', 'seven', 'selina'] >>> list1.remove('eric') >>> list1 ['anmi', 'kan', 'seven', 'selina'] 删除指定的所有元素 list1 = ['kan','seven','kan','eric','kan'] for i in range(list1.count('kan')): list1.remove('kan') print(list1)
9.反转
>>> list1 ['anmi', 'kan', 'seven', 'selina'] >>> list1.reverse() >>> list1 ['selina', 'seven', 'kan', 'anmi']
10.排序
>>> list1.sort() >>> list1 ['anmi', 'kan', 'selina', 'seven']
11.切片
>>> list1 = ['a','b','c','d','e'] >>> list1[0:3] #取列表前三个 ['a', 'b', 'c'] >>> list1[0:5:2] #取前五个,每隔一个 ['a', 'c', 'e'] >>> list1[-2:] #取最后两个 ['d', 'e']
12.扩展
>>> a = ['1','2','3'] >>> b = ['a','b','c'] >>> a.extend(b) >>> a ['1', '2', '3', 'a', 'b', 'c']