Python04
Python04列表列表的创建列表的特点获取指定元素的索引获取列表中的单个元素获取列表中多个元素--切片操作列表元素的查询操作列表元素的添加操作列表元素的删除操作列表元素的修改操作列表元素的排序操作列表生成式
列表
为什么需要列表
-
变量可以存储一个元素,而列表是一个“大容器”可以存储n多个元素,程序可以方便的对这些数据进行整体操作
-
列表相当于其他语言的数组
# 流程控制语句:break与continue在二重循环中的使用 for i in range(5): # 代表外层循环执行5次 for j in range(1,11): if j % 2 == 0: continue print(j, end='\t') print()
列表的创建
-
使用中括号
-
调用内置函数list()
'''创建列表的第一种方式,使用中括号''' lst = ['hello', 'world', 98] '''创建列表的第二种方式,使用内置函数list()''' lst2 = list(['hello', 'world', 98])
列表的特点
-
列表元素按顺序有序排序
-
索引映射唯一一个数据
-
列表可以存储重复数据
-
任意数据类型混存
-
根据需要动态分配和回收内存
'''创建列表的第一种方式,使用中括号''' lst = ['hello', 'world', 98, 'hello'] print(lst) print(lst[0], lst[-4]) '''创建列表的第二种方式,使用内置函数list()''' lst2 = list(['hello', 'world', 98])
获取指定元素的索引
-
如果列表中又多个相同元素,只返回列表中相同元素的第一个元素的索引
-
如果查询的元素在列表中不存在,则会出现ValueError
-
还可以在指定的start和stop之间进行查找
lst = ['hello', 'world', 98, 'hello'] print(lst.index('hello')) # print(lst.index('Python')) # ValueError: 'Python' is not in list # print(lst.index('hello', 1, 3)) # ValueError: 'hello' is not in list print(lst.index('hello', 1, 4)) # 3
获取列表中的单个元素
-
正向索引从0 ~ n-1
-
逆向索引从-n ~ -1
-
指定索引不存在,爆出indexError
lst = ['hello', 'world', 98, 'hello', 'world', 234] # 获取索引为2的元素 print(lst[2]) # 获取索引为-3的元素 print(lst[-3]) # 获取索引为10的元素 # print(lst[10]) # IndexError: list index out of range
获取列表中多个元素--切片操作
-
语法格式:列表名【start:stop:step】
-
step为负数时:切片的第一个元素默认是列表的最后一个元素;切片的最后一个元素默认是列表的第一个元素
lst = [10, 20, 30, 40, 50, 60, 70, 80] # start = 1, stop = 6, step = 1 # print(lst[1:6:1]) # [20, 30, 40, 50, 60] print("原列表:", id(lst)) lst2 = lst[1:6:1] print("切的片段:", id(lst2)) print(lst[1:6]) # [20, 30, 40, 50, 60] # 默认步长为1 print('------------step步长为正数的情况-----------') print(lst[1:6:]) # 默认步长为1 # start = 1, stop = 6, step = 2 print(lst[1:6:2]) # [20, 40, 60] # start 采用默认, stop = 6, step = 2 print(lst[:6:2]) # [10, 30, 50] # start 1, stop 采用默认, step = 2 print(lst[1::2]) # [20, 40, 60, 80] print('------------step步长为负数的情况-----------') print('原列表:', lst) print(lst[::-1]) # 将列表元素逆序输出 [80, 70, 60, 50, 40, 30, 20, 10] # start 7, stop 省略, step = -1 print(lst[7::-1]) # [80, 70, 60, 50, 40, 30, 20, 10] # start 6, stop 0, step = -2 print(lst[6:0:-2]) # [70, 50, 30]
列表元素的查询操作
-
判断指定的元素是否存在
print('p' in 'python') # True print('k' not in 'python') # True lst = [10, 20, 'python', 'hello'] print(10 in lst) # True print(100 in lst) # False print(10 not in lst) # False print(100 not in lst) # True
-
列表元素的遍历
lst = [10, 20, 'python', 'hello'] for item in lst: print(item)
列表元素的添加操作
-
append():在列表的末尾添加一个元素
-
extend():在列表的末尾至少添加一个元素
-
insert():在列表的任意位置添加一个元素
-
切片:在列表的任意位置添加至少一个元素
# 向列表的末尾添加一个元素 lst = [10, 20, 30] print('添加前:', lst) print(id(lst)) lst.append(100) print('添加后:', lst) print(id(lst)) lst2 = ['hello', 'world'] # lst.append(lst2) # 将lst2作为一个元素添加到列表的末尾 # print(lst) # [10, 20, 30, 100, ['hello', 'world']] lst.extend(lst2) # 向列表的末尾一次添加多个元素 print(lst) # [10, 20, 30, 100, 'hello', 'world'] lst.insert(1, 90) # 在任意位置上添加一个元素 print(lst) # [10, 90, 20, 30, 100, 'hello', 'world'] lst3 = [True, False, 'hello'] # 在任意的位置上添加n多个元素 lst[1:] = lst3 print(lst) # [10, True, False, 'hello']
列表元素的删除操作
-
remove():一次删除一个元素;重复元素只删除一个,元素不存在爆出ValueError
-
pop():删除一个指定索引位置上的一个元素;指定元素不存在则爆出IndexError;不指定元素位,则删除列表中最后一个元素
-
切片:一次至少删除一个元素,切片会产生一个新的列表对象
-
clear():清除列表
-
del:删除列表
lst = [10, 20, 30, 40, 50, 60, 30] lst.remove(30) # 从列表中移除一个元素,如果有重复元素,只移除第一个元素 print(lst) # [10, 20, 40, 50, 60, 30] # lst.remove(100) # ValueError: list.remove(x): x not in list # pop()根据索引移除元素 lst.pop(1) print(lst) # [10, 40, 50, 60, 30] # lst.pop(5) # IndexError: pop index out of range 如果指定索引位置不存在,则抛出异常 lst.pop() # 如果不指定参数(索引),将删除列表的最后一个元素 print(lst) print('---------------切片操作-删除至少一个元素,将产生一个新的列表对象-----------------') new_list = lst[1:3] print('原列表:',new_list) # 原列表: [40, 50] '''不产生新的列表对象,而是删除原列表的内容''' lst[1:3] =[] print(lst) # [10, 60] '''清楚列表中的所有元素''' lst.clear() print(lst) # [] '''del语句将列表对象删除''' del lst # print(lst) # NameError: name 'lst' is not defined
列表元素的修改操作
-
为指定的索引的元素赋予一个新值
-
为指定的切片赋予一个新值
lst = [10, 20, 30, 40] # 一次修改一个值 lst[2] = 100 print(lst) # [10, 20, 100, 40] lst[1:3] = [300, 400, 500, 600] print(lst) # [10, 300, 400, 500, 600, 40]
列表元素的排序操作
-
调用sort()方法,列表中所有元素默认按照从小到大的顺序进行排序,可以指定reverse = True,进行降序排序
-
调用内置函数sorted()。可以指定reverse = True,进行降序排序,原列表不发生改变
lst = [20, 40, 10, 98, 54] print('排序前的列表', lst, id(lst)) # 开始排序,调用列表对象的sort方法,默认升序 lst.sort() print('排序后的列表', lst, id(lst)) # [10, 20, 40, 54, 98] # 通过指定关键字参数,将列表中的元素进行降序排序 lst.sort(reverse=True) # reverse=True 表示降序排序 reverse=False 表示升序排序 print(lst) # [98, 54, 40, 20, 10] lst.sort(reverse=False) # reverse=True 表示降序排序 reverse=False 表示升序排序 print(lst) # [10, 20, 40, 54, 98] print('----------使用内置函数sorted()对列表进行排序,将产生一个新的列表对象--') lst = [20, 40, 10, 98, 54] print('排序前的列表', lst) # 排序前的列表 [20, 40, 10, 98, 54] # 开始排序 new_list = sorted(lst) print('排序后的列表', new_list) # 排序后的列表 [10, 20, 40, 54, 98] # 指定关键字参数,实现列表元素的降序排序 desc_list = sorted(lst, reverse=True) print(desc_list) # [98, 54, 40, 20, 10]
列表生成式
-
简称:生成列表的公式
-
注意:“表示列表元素的表达式”中通常包含自定义变量
lst = [i*i for i in range(1,10)] print(lst) # [1, 4, 9, 16, 25, 36, 49, 64, 81] '''列表中的元素为2,4,6,8,10''' lst2 = [2*i for i in range(1,6)] print(lst2) # [2, 4, 6, 8, 10]