python学习5-列表的创建、增删改查、排序

简介: python学习5-列表的创建、增删改查、排序

一、列表的创建方式

1、使用中括号
lst=['hello','world',98]
2、调用内置函数list()
lst2=list(["hello",'world',98])

二、列表的特点

  • 1、列表元素按顺序有序排序
  • 2、索引映射唯一一个数据
  • 3、列表可以存储重复数据
  • 4、任意数据类型混存
  • 5、根据需要动态分配和回收内存

三、列表的查询操作

1、获取列表中指定元素的索引 index()
#如果查找列表中有N个相同元素,只返回相同元素中的第一个元素的索引
lst=['hello','world',98,'hello']
print(lst.index('hello'))
#如果查询的元素在列表中不存在,则会抛出ValueError
print(lst.index('python'))
#还可以在指定的start和stop之间进行查找
print(lst.index('hello',1,3))  #ValueError: 'hello' is not in list   "world",98
print(lst.index("hello",1,4))   #"world",98,"hello"
2、获取列表中的单个元素
#正向索引从0~N-1  逆向索引从-N到-1 指定索引不存在,抛出IndexError
lst2=['hello','world',98,'hello','world',234]
#获取索引为2的元素
print(lst[2])
print(lst2[-4])
#获取索引为10的元素
print(lst2[10])  #IndexError: list index out of range
3、获取列表中的多个元素_切片操作 语法格式 列表名[start:stop:step]
  • step为正数
lst=[10,20,30,40,50,60]
print("原列表:",id(lst)) #1713813270848
lst2=lst[1:6:1]
print("切的片段:",id(lst2)) #1713808072640  切出来的是新的列表对象
print(lst[1:6])  #[20, 30, 40, 50, 60] 默认步长为1
print(lst[1:6:]) #也代表步长为1
  • step为负数
    切片的第一个元素默认是列表的最后一个元素
print(lst[::-1])  #[60, 50, 40, 30, 20, 10]
print(lst[6:0:-1]) #[60, 50, 40, 30, 20] 需要注意:stop的项是到此结束,但是不包括此项
4、判断指定元素在列表中是否存在 in/not in
print(10 in lst) #True
print("y" in lst) #False
5、列表元素的遍历 for…in
for item in lst:
    print(item)

四、列表元素的增加操作

1、append() 在列表的末尾添加一个元素
print("添加之前:",lst)  #[10, 20, 30, 40, 50, 60]
print(id(lst))
lst.append(100)
print("添加之后:",lst)  #[10, 20, 30, 40, 50, 60, 100]
print(id(lst))   #id相同,说明是同一个列表对象
2、extend() 在列表的末尾至少添加一个元素
lst2=['hello','world']
lst.append(lst2)   #不能将它直接print,它必须单独的写 [10, 20, 30, 40, 50, 60, 100, ['hello', 'world']]
lst.extend(lst2)   #[10, 20, 30, 40, 50, 60, 100, 'hello', 'world']
3、insert() 在列表的任意位置添加一个元素
lst.insert(1,90)  #在索引为1的位置上插入90这个元素 [10, 90, 20, 30, 40, 50, 60, 100, 'hello', 'world']
4、切片 在列表的任意位置添加至少一个元素 本质是替换
lst3=[True,False,'python']
lst[1:]=lst3 #[10, True, False, 'python']  从索引1开始之后的所有元素用新的列表元素替换

五、列表元素的删除操作(前四个是删除元素,最后一个del是删除列表)

1、remove() 一次删除一个元素,重复元素只删除第一个,元素不存在抛出ValueError
lst4=[10,20,30,40,50,20]
lst4.remove(10)   #[20, 30, 40, 50]
lst4.remove(100) #ValueError: list.remove(x): x not in list
lst4.remove(20) #[10, 30, 40, 50, 20]
2、pop() 删除一个指定索引位置上的元素,指定索引不存在抛出IndexError,不指定索引删除列表中最后一个元素
lst4.pop(1) #[10, 40, 50, 20]
lst4.pop(10) #IndexError: pop index out of range
lst4.pop() #[10, 30, 40, 50]
3、切片 一次至少删除一个元素,将产生一个新的列表对象
new_list=lst4[1:3]
print("切片前的列表:",lst4) #[10, 30, 40, 50]
print("切片后的列表:",new_list)  #[30, 40]
#不产生新的列表对象,而是删除原列表中的内容 本质是替换
lst4[1:3]=[]   #[10, 50] 把从索引1-2用空元素代替,相当于将中间部分切除
4、clear() 清空列表的所有元素
lst4.clear() #[]
5、del 删除列表 将列表对象删除
del lst4  #NameError: name 'lst4' is not defined

六、列表元素的修改操作

1、为指定索引的元素赋予一个新值
lst=[10,20,30,40]
lst[2]=100  #[10, 20, 100, 40]
2、为指定的切片赋予一个新值,修改多值
lst[1:3]=[100,200,300,400]   #[10, 100, 200, 300, 400, 40]

七、列表的排序操作(两种,sort()方法在原列表上改变,sorted()方法产生新的列表对象)

1、使用sort()方法,列表中的所有元素默认按照从小到大的顺序进行排序,可以指定reverse=True,进行降序排序
lst=[20,40,10,98,34]
print("排序前的列表:",lst)   #[20, 40, 10, 98, 34]
lst.sort()
print("排序后的列表:",lst)   #[10, 20, 34, 40, 98]  id相同,是在原列表的基础上进行的
lst.sort(reverse=True)   #reverse=True表示降序排序,reverse=False表示升序排序,默认是升序排序
print(lst) #[98, 40, 34, 20, 10]
2、调用内置函数sorted(),可以指定reverse=True,进行降序排序,原列表不发生改变(产生新的列表对象)
new_list=sorted(lst)
print(new_list)  #[10, 20, 34, 40, 98]
desc_list=sorted(lst,reverse=True)
print(desc_list) #[98, 40, 34, 20, 10]

八、列表生成式

lst=[i for i in range(1,10)]
print(lst)  #[1, 2, 3, 4, 5, 6, 7, 8, 9]
lst=[i*i for i in range(1,10)]  #表示列表元素的表达式中通常包含自定义变量,表示列表元素的表达式:i*i
print(lst)  #[1, 4, 9, 16, 25, 36, 49, 64, 81]
lst2=[i*2 for i in range(1,6)]
print(lst2) #[2, 4, 6, 8, 10]
相关文章
|
1天前
|
API 数据库 Python
Python web框架fastapi数据库操作ORM(二)增删改查逻辑实现方法
Python web框架fastapi数据库操作ORM(二)增删改查逻辑实现方法
|
2天前
|
索引 Python
python【列表】增删改查
python【列表】增删改查
|
2天前
|
Python
【Python21天学习挑战赛】- 错误和异常
【Python21天学习挑战赛】- 错误和异常
|
2天前
|
容器
【Python21天学习挑战赛】-迭代器 & f-格式化 & 模块
【Python21天学习挑战赛】-迭代器 & f-格式化 & 模块
|
2天前
|
Python
【Python21天学习挑战赛】- 函数进阶
【Python21天学习挑战赛】- 函数进阶
|
2天前
【Python21天学习挑战赛】文件读写操作
【Python21天学习挑战赛】文件读写操作
|
2天前
|
索引 Python
【Python21天学习挑战赛】集合 & 数据类型补充
【Python21天学习挑战赛】集合 & 数据类型补充
|
2天前
|
存储 缓存 Python
【Python21天学习挑战赛】字典 && 小数据池
【Python21天学习挑战赛】字典 && 小数据池
|
2天前
|
存储 索引 Python
【Python21天学习挑战赛】-列表 & 元组 & range
【Python21天学习挑战赛】-列表 & 元组 & range
|
2天前
|
Python
【Python21天学习挑战赛】-入门必备
【Python21天学习挑战赛】-入门必备