- append():原列表末尾处添加(扩展原列表,append里的参数必须是一个可迭代对象)
1 l_t = [1,2,2,34,4,5,5,6,0]
2 l_t.append('a')
3 print(l_t)
4 #结果:[1, 2, 2, 34, 4, 5, 5, 6, 0, 'a']
- 扩展原列表,append里的参数必须是一个可迭代对象
1 l_t = [1,2,2,34,4,5,5,6,0]
2 l_t.append(('s','q'))
3 print(l_t)
4 #结果:[1, 2, 2, 34, 4, 5, 5, 6, 0, ('s', 'q')]
1 l_t = [1,2,2,34,4,5,5,6,0]
2 l_t.clear()
3 print(l_t)
4 #结果:[]
1 l_t = [1,2,2,34,4,5,5,6,0]
2 temp = l_t.copy()
3 print(temp)
4 #结果:[1,2,2,34,4,5,5,6,0]
1 l_t = [1,2,2,34,4,5,5,6,0]
2 print(l_t.count(2))
3 #结果:2
- extend():将一个可迭代对象的元素遍历出来进行扩展
1 l_t = [1,2,2,34,4,5,5,6,0]
2 l_t.extend(('s','q'))
3 print(l_t)
4 #结果:[1, 2, 2, 34, 4, 5, 5, 6, 0, 's', 'q']
1 l_t = [1,2,2,34,4,5,5,6,0]
2 print(l_t.index(34))
3 #结果:3
1 l_t = [1,2,2,34,4,5,5,6,0]
2 l_t.insert(3,'sb')
3 print(l_t)
4 #结果:[1, 2, 2, 'sb', 34, 4, 5, 5, 6, 0]
- pop():删除列表中某一元素(1.可指定位置 2.默认为最后一个),可用变量获取获取该值
1 l_t = [1,2,2,34,4,5,5,6,0]
2 l_t.pop()
3 print(l_t)
4 #结果:[1, 2, 2, 34, 4, 5, 5, 6]
1 l_t = [1,2,2,34,4,5,5,6,0]
2 l_t.pop(2)
3 print(l_t)
4 #结果:[1, 2, 34, 4, 5, 5, 6, 0]
1 l_t = [1,2,2,34,4,5,5,6,0]
2 a = l_t.pop(2)
3 print(a)
4 #结果:2
1 l_t = [1,2,3,4,5,6,7,8,9,'sb']
2 l_t.remove('sb')
3 print(l_t)
4 #结果:[1, 2, 3, 4, 5, 6, 7, 8, 9]
1 l_t = [1,2,3,4,5,6,7,8,9,'sb']
2 del l_t[-1]
3 print(l_t)
4 #结果:[1, 2, 3, 4, 5, 6, 7, 8, 9]
1 l_t = [1,2,3,4,5,6,7,8,9,]
2 l_t.reverse()
3 print(l_t)
4 #结果:[9, 8, 7, 6, 5, 4, 3, 2, 1]
1 l_t = [9, 8, 7, 6, 5, 4, 3, 2, 1]
2 l_t.sort()
3 print(l_t)
4 #结果:[1, 2, 3, 4, 5, 6, 7, 8, 9]
1 l_t = [1,2,3,4,5,6,7,8,9,]
2 l_t.sort(reverse=True)
3 print(l_t)
4 #结果:[9, 8, 7, 6, 5, 4, 3, 2, 1]