列表是用方括号标注、逗号分隔的一组值,可以包含不同类型的元素:
列表内的数据有先后顺序关系
列表是可变的容器
列表创建
l = [] #空列表
x = [1,2,3,4]
y = [1, 2, [3.1, 3.2], 4] # 含有四个元素的列表,第三个元素是列表
L2 = [
['姓名','语文成绩','数学成绩'],
['小王', 90, 100],
['牛犇', 59, 26]
]
函数函数list
list() # 创建一个空的列表,等同于 []
list(可迭代对象) # 用可迭代对象创建一个列表
L = list() # L = []
L = list("ABC") # L = ['A', 'B', 'C']
L = list(range(5)) # L = [0, 1, 2, 3, 4]
列表运算
列表list同字符串str都是序列, 他们的运算规则基本相同。
-
+ 用于拼接列表
x = [1, 2, 3]
y = [4, 5, 6]
z = x + y # [1, 2, 3, 4, 5, 6]
+= 追加
L = [1, 2, 3]
L += [4, 5] # L = [1, 2, 3, 4, 5]
L = [1, 2, 3]
L += "ABC" # L = [1, 2, 3, 'A', 'B', 'C']
L += range(2)
重复的列表
list = [1, 2, 3, 4, 5]
list *=3
print(list) # [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
== 、!= 用于比较
l = [1, 2, 3, 4, 5]
l2 = [1, 2, 3, 4, 5]
l3 = [1, 2, 3, 4, 6]
print(l == l2) # True
print(l == l3) # False
in 、not in 用于判断一个数据元素是否在列表中
l =['apple', 'banana', 'orange', 'grape']
x = 'apple' in l #判断元素是否在列表中
print(x) # True
x1 = 'pear' in l
print(x1) # False
x2 = 'pear' not in l
print(x2) # True
列表访问
与字符串的索引一样,列表索引从 0 开始,第二个索引是 1,依此类推。
l =['apple', 'banana', 'orange', 'grape']
print(l[0]) # apple
索引也可以从尾部开始,最后一个元素的索引为 -1,往前一位为 -2,以此类推。
l =['apple', 'banana', 'orange', 'grape']
print(l[-1]) # 输出列表l的最后一个元素
切片
列表[(开始索引b):(终止索引e)(: (步长s))]
含开始索引对应值。不含结束索引对应值。步长就是间隔多少取一个值,为复数就反向。
l =['apple', 'banana', 'orange', 'grape']
print(l[:2]) # ['apple', 'banana']
print(l[2:]) # ['orange', 'grape']
print(l[1:3]) # ['banana', 'orange']
print(l[::2]) # ['apple', 'orange']
print(l[::-1]) # ['grape', 'orange', 'banana', 'apple']
列表操作
增、删、改、查
官方文档:https://docs.python.org/zh-cn/3.13/library/stdtypes.html#sequence-types-list-tuple-range

-
append(x): 在列表末尾添加一个元素x。 -
insert(i, x): 在索引i位置插入元素x。 -
extend(iterable): 将可迭代对象iterable中的所有元素添加到列表末尾。 -
remove(x): 移除列表中第一个值为x的元素。 -
pop([i]): 移除并返回索引i的元素,默认移除并返回最后一个元素。 -
clear(): 移除列表中的所有元素。 -
len(list)列表元素个数
-
max(list)返回列表元素最大值
-
min(list)返回列表元素最小值
-
list(seq)将元组转换为列表
-
index(x[, start[, end]]): 返回列表中第一个值为x的元素的索引。 -
count(x): 返回列表中值为x的元素的数量。 -
sort(key=None, reverse=False): 对列表进行原地排序。 -
reverse(): 对列表进行原地反转。 -
copy(): 返回列表的一个浅复制。
添加数据
append(x):
在列表末尾添加一个元素 x。
l =['apple', 'banana', 'orange', 'grape']
l.append("123")
print(l) # ['apple', 'banana', 'orange', 'grape', '123']
insert(i, x):
在索引 i 位置插入元素 x。
l =['apple', 'banana', 'orange', 'grape']
l.insert(1,"123")
print(l) # ['apple', '123', 'banana', 'orange', 'grape']
list1.extend(list2):
将可迭代对象 list2中的所有元素添加到list1列表末尾。
l =['apple', 'banana', 'orange', 'grape']
l.extend(['123', '456'])
print(l) # ['apple', 'banana', 'orange', 'grape', '123', '456']
修改内容
l =['apple', 'banana', 'orange', 'grape']
l[1] = '123'
print(l) # ['apple', '123', 'orange', 'grape']
删除数据
remove(x):
移除列表中第一个值为 x 的元素。
l =['apple', 'banana', 'orange', 'grape']
l.remove('apple') #删除apple
print(l) # ['banana', 'orange', 'grape']
pop([i]):
移除并返回索引 i 的元素,默认移除并返回最后一个元素。
l =['apple', 'banana', 'orange', 'grape']
l.pop(2) #删除索引为2的元素
print(l) # ['apple', 'banana', 'grape']
clear():
移除列表中的所有元素(清空)。
l =['apple', 'banana', 'orange', 'grape']
l.clear() #清空列表
print(l) # []
del
语句删除指定位置的数据元素
l =['apple', 'banana', 'orange', 'grape']
del l[0] #删除索引为0的元素
print(l) # ['banana', 'orange', 'grape']
del l #删除列表, 释放内存,删除变量l
常见API
通用操作:https://docs.python.org/zh-cn/3.13/library/stdtypes.html#common-sequence-operations
可变容器操作:https://docs.python.org/zh-cn/3.13/library/stdtypes.html#mutable-sequence-types
len(list)
返回列表的元素个数
l =['apple', 'banana', 'orange', 'grape']
print(len(l)) # 4
man(list)
返回列表元素中最大值
l =[1, 2, 3, 4, 5]
print(max(l)) #5
min(list)
返回列表元素最小值
l =[1, 2, 3, 4, 5]
print(min(l)) #1
list.index(x)
返回列表中第一个值为 x 的元素的索引。
l = [1, 2, 3, 4, 5,1,2,3,4,5]
print(l.index(5)) # 4
list.count(x)
返回列表中值为 x 的元素的数量。
l = [1, 2, 3, 4, 5,1,2,3,4,5,5]
print(l.count(5)) # 3
list.sort()
对列表进行原地排序
l = [1, 2, 3, 4, 5,1,2,3,4,5,5]
#排序
l.sort()
print(l) # [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5]
list.reverce()
对列表进行原地反转。
与list[::-1]效果相同
l = [5,1,4,2,9,7]
l1 = l[::-1] #将列表l反转
print(l1)
l.reverse() #将列表l反转
print(l)
x = l == l1 #判断l是否和l1相等
print(x)
9124

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



