# append() 追加 【在原来值最后追加】
test = [1,2,3,[88,99],'abc']
test.append('')
print(test) [1, 2, 3, [88, 99], 'abc', '']
# clear() 清空
test = [12, '好', 'abc']
test.clear()
print(test) []
# copy() 浅拷贝
test = [12, '好', 'abc']
v = test.copy()
print(v) [12, '好', 'abc']
# count() 计算出指定元素出现的次数
test = [12,34,8,56,8,98]
v = test.count(8)
print(v) 2
# extend() 扩展原来的列表 参数:可迭代对象
test = [12,23,18,19]
test.extend([8,66])
print(test) [12, 23, 18, 19, 8, 66] test = [12,23,18,19]
test.extend('早上好')
print(test) [12, 23, 18, 19, '早', '上', '好']
# index() 根据值获取当前值索引位置(最左边优先)
test = [12,22,34,51,22,88]
v = test.index(22)
print(v) 1
# insert() 在指定位置插入元素
test = [12,22,34,51,22,88]
test.insert(0,8)
print(test) [8, 12, 22, 34, 51, 22, 88]
# pop() 删除某个值(从最后一个开始删),并获取删除的值
test = [12,22,34,51,22,88]
v = test.pop()
print(test)
print(v) [12, 22, 34, 51, 22]
88 # pop(位置数) 删除指定位置的值
test1 = [12,22,34,51,22,88]
v1 = test1.pop(2)
print(test1)
print(v1) [12, 22, 51, 22, 88]
34
# remove() 删除指定的值,左边优先
test = [12,22,34,51,22,88]
test.remove(34)
print(test) [12, 22, 51, 22, 88]
ps: 删除的方法 pop() remove() del() del test[2:5] clear()
# reverse() 将当前列表反转
test = [1,2,3,4,5]
test.reverse()
print(test) [5, 4, 3, 2, 1]
# sort() 排序 (从小到大)
test = [2,9,5,7,4,6,0]
test.sort()
print(test) [0, 2, 4, 5, 6, 7, 9] test1 = [2,9,5,7,4,6,0]
test1.sort(reverse=True) #(从大到小)
print(test1) [9, 7, 6, 5, 4, 2, 0]