目录-列表
列表
变量可以存储一个元素,而列表是一个大容器,可以存储N多个元素,程序可以方便地对这些数据进行整体操作。列表相当于数组。
列表特点
- 列表元素按顺序有序排序【有序】,且索引映射唯一数据
- 列表可以存重复数据,任意数据类型混存
- 根据需要动态分配和回收内存【可变序列】
列表创建
列表的创建有两种方法
1、使用中括号创建列表,符号位[ ]。
list1 = [1,2,3,4]
print(type(list1)) # <class 'list'>
print(list1) # [1, 2, 3, 4]
2、调用内置函数list()创建列表。
list2 = list(range(1,5))
print(type(list2)) # <class 'list'>
print(list2) # [1, 2, 3, 4]
3、创建空列表
# 通过[]创建空列表
list1 = []
print(type(list1)) # <class 'list'>
print(list1) # []
# 通过list创建空列表
list2 = list()
print(type(list2)) # <class 'list'>
print(list2) # []
列表查询
查询列表有5中方法:
index
index(value)–获取指定元素的索引值,且只能返回第一个,元素不存在则会报错。
# index
list1 = [23,45,32,90,56,32]
#查找元素90的索引
print(list1.index(90)) # 3
#当查找的元素不存在时报错
# print(list1.index(100)) # ValueError: 100 is not in list
#当查找的元素存在多个时,返回第一个元素的索引
print(list1.index(32)) # 2
#当想要获取第二个元素的索引时,index还有另外两个参数start和end,指定搜索范围
print(list1.index(32,3)) # 从索引为3的位置进行开始搜索32这个元素,在索引为5的位置上
list[]-单个元素
list[]–可以通过索引获取列表中单个元素,也可以获取多个元素,可以为正数,也可以为负数,超过索引范围则会报错。
比如下面列表:[34,56,23,67,90],从右往左数,索引则从0开始,34的索引为0,90的索引为4。从左往右数,索引则从-1开始,90的索引为-1,34的索引为-5。
# list[]
list1 = [23,45,32,90,56]
#获取第一个元素
print(list1[0]) # 23
#索引越界,报错
print(list1[10]) # IndexError: list index out of range
list[]-切片
list[]–可以获取多个元素,列表[start:stop:step],start–代表从哪个索引开始,stop–代表从哪个索引介绍但不包括这个索引,step–代表步长。
list2 = list(range(10))
print(list2) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#获取从索引4开始,到结尾的切片
print(list2[4:]) # [4, 5, 6, 7, 8, 9]
# 获取从索引3到索引6的切片,但是不包括索引6的元素
print(list2[3:6]) # [3, 4, 5]
# 从索引2到索引7结束,步长为2
print(list2[2:7:2]) # [2, 4, 6]
print(list2[-1:-8:-3]) # [9, 6, 3]
in/not in
判断元素是否在列表中,使用in/not in
list2 = list(range(10))
print(list2) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(9 in list2) # True
print(9 not in list2) # False
遍历列表
list2 = list(range(10))
print(list2) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 遍历列表
for i in list2:
print(i)
列表增加
append-末尾添加
append()–向列表的末尾添加一个元素
list1 = ['123',89,'abdc',True]
list1.append('zhang')
print(list1) # ['123', 89, 'abdc', True, 'zhang']
extend-增加其他列表元素
extend()–在列表的末尾至少添加一个元素
list1 = ['123',89,'abdc',True]
list1.extend(['hello','world'])
print(list1) # ['123', 89, 'abdc', True, 'hello', 'world']
insert-任意位置插入元素
insert()–在列表的任意位置插入元素
list1 = ['123',89,'abdc',True]
#在列表第一个位置插入元素33
list1.insert(0,33)
print(list1) # [33, '123', 89, 'abdc', True]
# 在列表最后一个位置插入元素99
list1.insert(len(list1),99)
print(list1) # [33, '123', 89, 'abdc', True, 99]
切片
切片–在任意位置上添加N个元素
list1 = ['123',89,'abdc',True]
list1[4:] = [1,2,3,4]
print(list1) # ['123', 89, 'abdc', True, 1, 2, 3, 4]
列表删除
remove-删除某个元素
remove()–从列表中移除一个元素,如果有重复元素只移第一个元素,移除不存在的元素会报错
list1 = ['123',89,'abdc',True,99,'abdc']
# 移除元素89
print(list1.remove(89)) # None
print(list1) # ['123', 'abdc', True, 99, 'abdc']
# 移除元素abdc时,只移除了第一个abdc元素
list1.remove('abdc')
print(list1) # ['123', True, 99, 'abdc']
# 移除不存在的元素报错
# list1.remove('eeee') # ValueError: list.remove(x): x not in list
pop-移除末尾元素/根据索引移除
pop()–根据索引移除元素,超出索引位置时会报错,如果不指定惨数则会移除最后一个元素。
list1 = ['123',89,'abdc',True,99,'abdc']
# 移除第2个元素-89
print(list1.pop(1)) # None
print(list1) # ['123', 'abdc', True, 99, 'abdc']
# pop不指定参数,移除最后一个元素
list1.pop()
print(list1) # ['123', 'abdc', True, 99]
# 移除不存在的索引,报错
list1.pop(100) # IndexError: pop index out of range
切片-移除元素
list1 = ['123',89,'abdc',True,99,'abdc']
list1[2:] = []
print(list1) # ['123', 89]
clear-清空列表
clear()—删除列表的所有元素
list1 = ['123',89,'abdc',True,99,'abdc']
list1.clear()
print(list1) # []
del-删除列表对象
del----删除列表对象
list1 = ['123',89,'abdc',True,99,'abdc']
del list1 #删除内存中list1变量
print(list1) # NameError: name 'list1' is not defined
列表修改
修改指定索引的元素
list1[2]=100----为指定索引的元素赋予一个新的值
list1 = ['123',89,'abdc',True,99,'abdc']
list1[2] = 100
print(list1) # ['123', 89, 100, True, 99, 'abdc']
通过切片修改元素
list1[1:3]=[100,200,300,400]–为指定的切片赋予一个新的值
list1 = ['123',89,'abdc',True,99,'abdc']
list1[1:3]=[100,200,300,400]
print(list1) # ['123', 100, 200, 300, 400, True, 99, 'abdc']
列表排序
sort–排序
list1.sort(reverse=True/False)–当没有reverse参数,默认升序【reverse=False】,当reverse=True时,降序排序
list1 = [99,23,78,45,34,89]
# 按照升序排序,且sort会改变列表本来的排序
list1.sort()
print(list1) # [23, 34, 45, 78, 89, 99]
# 按照降序排序
list1.sort(reverse=True)
print(list1) # [99, 89, 78, 45, 34, 23]
#reverse为False时,为升序,所以默认为升序
list1.sort(reverse=False)
print(list1) # [23, 34, 45, 78, 89, 99]
内置函数sorted()
内置函数Sorted()—支持升序或者降序排序,不会改变原来的列表的顺序。
list1 = [99,23,78,45,34,89]
## sorted默认升序排序,也可以增加参数rever
### 不加参数,默认升序排序
list2 = sorted(list1)
print(list2) # [23, 34, 45, 78, 89, 99]
### 加参数reverse=False,也是升序排序
list3 = sorted(list1,reverse=False)
print(list3) # [23, 34, 45, 78, 89, 99]
## 降序排序
list4 = sorted(list1,reverse=True)
print(list4) # [99, 89, 78, 45, 34, 23]
## 使用sorted排序不会改变原来列表的顺序
print(list1) # [99, 23, 78, 45, 34, 89]
列表生成式
列表生成式–生成列表的公式。

# 使用列表生成式生成2,4,6,8,10元素的列表
list1 = [2*i for i in range(1,6)]
print(list1) # [2, 4, 6, 8, 10]
本文详细介绍了Python中的列表,包括列表的特点、创建、查询、增加、删除、修改、排序以及列表生成式等操作。重点讲解了如何通过索引、切片、函数和方法对列表元素进行操作,为Python编程提供了基础支持。

被折叠的 条评论
为什么被折叠?



