列表list列表能够存储多个不同类型的数据,存储的是每一个数据对象的援用,相似于其余语言的数组概念创立列表,两种形式应用[]lst = ['1', '2']应用内置函数list()lst2 = list(['1', '2'])列表的特点列表数据有序排列索引映射惟一一个数据,索引从左到右,是从0开始,索引从右到左,是从-1开始能够存储反复的数据任意类型数据混存依据须要动态分配和回收内存index(): 获取某列表元素在列表中的索引list1 = ['hello1', 'hello2', 'hello3', 'hello']print(list1.index('hello')) # 只返回找到的第一个元素的索引# print(list1.index('hell')) # 若元素不存在,抛出ValueErrorprint(list1.index('hello', 1, 4)) # 可指定查找范畴,不包含end依据索引获取列表中某个元素print(list1[-2]) # 输入‘hello3’print(list1[3]) # 输入‘hello’# print(list1[-7]) # 索引超出范围,抛出异样IndexError获取列表中的多个元素:切片操作语法:列表名[start: stop: step] start:默认为0 stop:不包含stop索引元素,不写截到开端 step:默认为1list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]# 对原列表的拷贝,生成新列表和原列表援用id不一样print(list2[1:5:1]) # 输入[2, 3, 4, 5]print(list2[:2]) # 输入[1, 2]print(list2[::]) # 输入[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]print(list2[1::]) # 输入[2, 3, 4, 5, 6, 7, 8, 9, 10]# 步长为正数,倒序切片输入print(list2[::-1]) # 输入[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]print(list2[10::-1]) # 输入[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]print(list2[5::-1]) # 输入[6, 5, 4, 3, 2, 1]print(list2[::-2]) # 输入[10, 8, 6, 4, 2]判断某元素是否存在在列表中,in / not inprint(1 in list2) # Trueprint(1 not in list2) # False遍历列表的元素for item in list2: print(item, end='\t') # 输入[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]列表元素的增删改操作减少操作1. append():列表开端追加一个元素list2.append(11)print(list2)# 输入[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]2. extend():列表开端追加一个或多个元素list2.extend(list1)print(list2)# 输入[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 'hello1', 'hello2', 'hello3', 'hello']3. insert():在任意地位插入一个元素,之后的元素程序后移list2.insert(1, 122)print(list2)# 输入[1, 122, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 'hello1', 'hello2', 'hello3', 'hello']4. 切片操作:在任意地位增加多个元素,先切片后替换被切掉的局部list2[:5:] = list1print(list2)# 输入:['hello1', 'hello2', 'hello3', 'hello', 5, 6, 7, 8, 9, 10, 11, 'hello1', 'hello2', 'hello3', 'hello']删除操作1.remove():删除一个指定元素list2.remove('hello') # 只删除第一个,不存在则报错ValueErrorprint(list2) # 输入['hello1', 'hello2', 'hello3', 5, 6, 7, 8, 9, 10, 11, 'hello1', 'hello2', 'hello3', 'hello']2.pop():依据索引删除元素,不写索引默认删除末位元素,指定索引不存在,抛出异样IndexErrorlist2.pop(1)print(list2)# 输入['hello1', 'hello3', 5, 6, 7, 8, 9, 10, 11, 'hello1', 'hello2', 'hello3', 'hello']3.切片操作:一次至多删除1个元素,产生新的列表对象newList = list2[1:3]print(newList) # 输入['hello3', 5]# 不生成新对象,操作原数组list2[1:3] = []print(list2) # 输入['hello1', 6, 7, 8, 9, 10, 11, 'hello1', 'hello2', 'hello3', 'hello']4.clear():清空列表list2.clear()print(list2) # 输入[]5.del:删除列表del list2# print(list2) # NameError: name 'list2' is not defined批改操作1. 批改指定索引的元素list2 = [1, 2, 3, 4, 5]list2[2] = 100print(list2) # 输入[1, 2, 100, 4, 5]2. 切片替换list2[1:2] = [10, 20, 30]print(list2) # 输入[1, 10, 20, 30, 100, 4, 5]排序操作1.sort(): 默认从小到大排序,指定reverse=True,降序,操作的是原列表list3 = [11, 15, 2, 8, 5, 4, 6, 3]list3.sort()print(list3) # [2, 3, 4, 5, 6, 8, 11, 15]list3.sort(reverse=True)print(list3) # [15, 11, 8, 6, 5, 4, 3, 2]2.sorted():内置函数;默认从小到大排序,指定reverse=True,降序;原列表不变,生成新列表list4 = [11, 15, 2, 8, 5, 4, 6, 3]nl = sorted(list4)print(nl) # [2, 3, 4, 5, 6, 8, 11, 15]nl = sorted(list4, reverse=True)print(nl) # [15, 11, 8, 6, 5, 4, 3, 2]列表生成式(列表元素有规定)语法:[i*i for i in range(1, 10)]i*i:列表元素的表达式,通常包含自定义变量i:自定义变量range(1, 10):可迭代的对象list5 = [i*i for i in range(1, 10)]print(list5) # [1, 4, 9, 16, 25, 36, 49, 64, 81]list6 = [i*2 for i in range(1, 6)]print(list6) # [2, 4, 6, 8, 10]