列表的增、删、改、查
l1 = ["a","b","c",1,2]
print(l1[-1:1:-1]) [2, 1, 'c']
#增
l1.append("summer") #增加到列表的最后位置
l1.insert(1,"aaaa") #根据索引添加元素
l1.extend([3,4,5,6]) #将序列里面的元素迭代的添加到列中
print(l1) ['a', 'b', 'c', 1, 2, 3, 4, 5, 6]
l1.extend(["rain",["egon","23"]])
print(l1) ['a', 'b', 'c', 1, 2, 'rain', ['egon', '23']]
#删除
# res = l1.pop() #删除最后一个,有返回值
print(res)
# res = l1.pop(2) #按照索引删除,有返回值
# res = l1.remove("a") #按照元素删除,没有返回值
# l1.clear() #清空数据
#del删除
#del l1[0] #按照索引删除
# del l1[:2] #切片删除
#del l1 #将l1从内存中删除
#改
1、通过索引直接修改
l1[1] = "alex"
2、通过切片修改或者添加
# l = ["a","b","c","d","e","f"]
# l[0] = [1,2,3] #直接将列表添加到索引位置处
# l[1] = {"name":"summer"}
# print(l) ['a', {'name': 'summer'}, 'c', 'd', 'e', 'f']
# l[:2] = [1,2,3,4] #切片后将数据迭代的添加到被切除的位置
# print(l) [1, 2, 3, 4, 'c', 'd', 'e', 'f']
# l[0:1] = [9,8,7]
# print(l) [9, 8, 7, 'b', 'c', 'd', 'e', 'f']
# l[1] = ["summer","rain"]
# print(l)
# l[0:4:2] = [11,22] #切片加步长。切了多少个元素就要相应的添加多少个元素,否则直接报错
# print(l) [11, 'b', 22, 'd', 'e', 'f']
##排序
l1 = [1,3,4,23,76,21,54,56,12]
l1.sort() #当列表里面的元素全部是由数字组成时,从小到大排序
print(l1) [1, 3, 4, 12, 21, 23, 54, 56, 76]
l1.sort(reverse=True) #从大到小排序
print(l1) [76, 56, 54, 23, 21, 12, 4, 3, 1]
l1.reverse() #将列表里面的元素的顺序翻转
print(l1) [12, 56, 54, 21, 76, 23, 4, 3, 1]
print(l1[::-1]) #切片反序 [12, 56, 54, 21, 76, 23, 4, 3, 1]