Part1 列表删除
练习:删除指定分数列表中所有低于60分的成绩
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
删除后: [98, 89, 67, 100, 78]
坑一:直接遍历用remove删除元素 - 删不干净(因为遍历的时候没有把所有元素都遍历出来)
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
for score in scores:
print('score:', score)
if score < 60:
scores.remove(score)
print(scores) # [98, 34, 89, 67, 9, 100, 78]
运算过程
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
score = 98: 98<60 -> False
score = 45: 45<60 -> True:scores.remove(45) -> scores = [98, 34, 89, 23, 67, 23, 9, 54, 100, 78]
score = 89: 89<60 -> False
score = 23: 23<60 -> True:scores.remove(23) -> scores = [98, 34, 89, 67, 23, 9, 54, 100, 78]
score = 23: 23<60 -> True: scores.remove(23) -> scores = [98, 34, 89, 67, 9, 54, 100, 78]
score = 54: 54<60 -> True: scores.remove(54) -> scores = [98, 34, 89, 67, 9, 100, 78]
score = 78: 78<60 -> False
循环结束
print(scores) -> [98, 34, 89, 67, 9, 100, 78]
解决方法:创建一个和原列表一样的新列表,遍历新列表,删除原列表
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
scores1 = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
scores1 = scores.copy() # scores1 = scores[:]; scores1 = scores*1; scores1=scores+[]
for s in scores1:
if s < 60:
scores.remove(s)
print(scores)
坑二:报错(下标越界)
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
、len(列表) - 获取列表中元素的个数
、for index in range(len(scores)):
print('index:', index)
if scores[index] < 60:
del scores[index]# print(scores)
解决方法:
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
index = 0
while index < len(scores):
s = scores[index]
if s < 60:
del scores[index]
else:
index += 1
print(scores) # [98, 89, 67, 100, 78]
运算过程
[98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
index = 0 98
index = 1 45 -> [98, 34, 89, 23, 67, 23, 9, 54, 100, 78]
index = 1 34 -> [98, 89, 23, 67, 23, 9, 54, 100, 78]
index = 1 89
index = 2 23 -> [98, 89, 67, 23, 9, 54, 100, 78]
index = 2 67
index = 3 23 -> [98, 89, 67, 9, 54, 100, 78]
index = 3 9 -> [98, 89, 67, 54, 100, 78]
index = 3 54 -> [98, 89, 67, 100, 78]
index = 3 100
index = 4 78
(循环结束)
Part 2 列表的切片
列表切片 - 获取列表中部分元素(一次性获取多个元素)
1.基本语法
列表[开始下标:结束下标:步长] - 从开始下标开始获取到结束下标前为止,每次下标值增加步长去获取下一个元素
注意:
1)列表切片的结果一定是列表
2)结束下标对应的元素一定取不到
3)
a. 如果步长为正,表示从前往后取(从开始下标到结束下标),这个时候开始下标对应的元素必须在结束下标对应的元素的前面,否则结果是空
b. 如果步长为负,表示后后往前取(从开始下标到结束下标),这个时候开始下标对应的元素必须在结束下标对应的元素的后面,否则结果是空
list1 = [23, 45, 67, 89, 45, 67, 32]
print(list1[1:4:1]) # [45, 67, 89]
print(list1[0:6:2]) # [23, 67, 45]
print(list1[2:5:1]) # [67, 89, 45]
print(list1[3:3:1]) # []
print(list1[1:4:-1]) # []
print(list1[1:-1:-1]) # []
print(list1[-2:1:-2]) # [67, 89]
2.切片省略
1)省略步长
列表[开始下标:结束下标] - 省略步长,步长就是1:列表[开始下标:结束下标:1]
fruits = ['苹果', '香蕉', '猕猴桃', '橘子', '石榴', '葡萄']
print(fruits[1:4]) # ['香蕉', '猕猴桃', '橘子']
print(fruits[-3:-1]) # ['橘子', '石榴']
print(fruits[-1:2]) # []
2)省略开始下标
列表[:结束下标:步长]
步长为正:从第一个元素开始往后取(相当于开始下标是0)
步长为负:从最后一个元素开始往前取(相当于开始下标是-1)
fruits = ['苹果', '香蕉', '猕猴桃', '橘子', '石榴', '葡萄']
print(fruits[:4]) # fruits[1:4:1] -> ['苹果', '香蕉', '猕猴桃', '橘子']
print(fruits[:3:-2]) # fruits[-1:3:-2] -> ['葡萄']
print(fruits[:2:-1]) # ['葡萄', '石榴', '橘子']
print(fruits[:3:2]) # ['苹果', '猕猴桃']
3)省略结束下标
列表[开始下标::步长]
步长为正:取到最后一个元素为止
步长为负:取到第一个元素为止
fruits = ['苹果', '香蕉', '猕猴桃', '橘子', '石榴', '葡萄']
print(fruits[2:]) # ['猕猴桃', '橘子', '石榴', '葡萄']
print(fruits[-3::-1]) # ['橘子', '猕猴桃', '香蕉', '苹果']
print(fruits[3::-2]) # ['橘子', '香蕉']
print(fruits[:]) # ['苹果', '香蕉', '猕猴桃', '橘子', '石榴', '葡萄']
print(fruits[::-1]) # ['葡萄', '石榴', '橘子', '猕猴桃', '香蕉', '苹果']
print('hello'[1:4]) # 'ell'
print('hello'[::-1]) # 'olleh'
Part3 列表相关操作
1.加法运算和乘法运算
1)加法:列表1+列表2 - 合并两个列表产生一个新的列表
list1 = [10, 20, 30]
list2 = [100, 200]
list3 = list1 + list2
print(list3) # [10, 20, 30, 100, 200]
注意:列表只能和列表相加
print(list1 + 100) # TypeError: can only concatenate list (not "int") to list
2)乘法:列表N / N列表 - 将列表中的元素重复N次产生一个新的列表
list4 = list1 * 3
print(list4) # [10, 20, 30, 10, 20, 30, 10, 20, 30]
list5 = list1*1
list6 = list1
print(list5, list6) # [10, 20, 30] [10, 20, 30]
print(id(list1), id(list5), id(list6)) # 4377817568 4378998656 4377817568
2.比较运算
两个列表之间支持比较大小和相等
1)两个列表比较大小 - 比较第一对不相等的元素的大小,谁大对应的列表就大
list11 = [10, 200, 3, 4, 5]
list22 = [10, 20, 30000]
print(list11 > list22)
print([1, '2', 3] > [1, 1, 2]) # TypeError: '>' not supported between instances of 'str' and 'int'
2)比较相等
元素顺序不同的两个列表不相等
print([1, 2, 3] == [1, 2, 3]) # True
print([1, 3, 2] == [1, 2, 3]) # False
print([1, 2, 3] == 'abc') # False
print({1, 2, 3} == {3, 1, 2}) # True
print({'a': 10, 'b': 20})
3. in 和 not in
元素 in 列表 - 判断列表中是否存在指定的元素
元素 not in 列表 - 判断列表中是否不存在指定的元素
print(10 in [1, 20, 10, 4]) # True
print(10 not in [1, 20, 10, 4]) # False
print(10 in [1, 2, 3, [10, 20]]) # False
print([1, 2] in [1, 2, 3, 4]) # False
print([1, 2] in [[1, 2], 3, 4]) # True
print([1, 2] in [[1, 2]]) # True
练习:获取两个列表中功能元素
A = [1, 2, 5, 10, 3, 2]
B = [5, 2, 10, 20, 32, 2]
求公共列表 C : [5, 2, 10]
A = [1, 2, 5, 10, 3, 2]
B = [5, 2, 10, 20, 32, 2]
C = []
for x in A:
if x in B and x not in C:
C.append(x)
print(C) # [2, 5, 10]
4.相关函数
sum、max、min、sorted、len、list
1)sum(数字列表) - 求列表中所有元素的和
scores = [34, 89, 90, 78, 65, 78, 60]
print(sum(scores)) # 494
2).max(列表)/min(列表) - 求列表中元素的最大/最小值(注意:列表中元素的类型必须一致,并且元素本身支持比较运算)
print(max(scores)) # 90
print(min(scores)) # 34
3).
sorted(列表) - 将列表中的元素从小到大排序(升序),产生一个新的列表(不会修改原列表)
sorted(列表, reverse=True) - 将列表中的元素从大到小排序(降序), 产生一个新的列表(不会修改原列表)
scores = [34, 89, 90, 78, 65, 78, 60]
new_scores = sorted(scores)
print(new_scores) # [34, 60, 65, 78, 78, 89, 90]
scores = [34, 89, 90, 78, 65, 78, 60]
new_scores = sorted(scores, reverse=True)
print(new_scores) # [90, 89, 78, 78, 65, 60, 34]
不会修改原列表
print(scores) # [34, 89, 90, 78, 65, 78, 60]
- len(列表) - 获取列表长度(列表中元素的个数)
print(len(scores)) # 7
- list(数据) - 将指定数据转换成列表(数据必须是序列;转换的时候直接将序列中的元素作为新的列表的元素)
print(list('abc')) # ['a', 'b', 'c']
print(list(range(4))) # [0, 1, 2, 3]
Part4 列表相关方法
1.列表.clear() - 清空指定列表
names = ['犬夜叉', '火影忍者', '海贼王']
names.clear()
print(names) # []
2.列表.copy() - 复制指定列表产生一个一模一样的新列表(地址不同)
这儿是浅拷贝
names = ['犬夜叉', '火影忍者', '海贼王']
new_names = names.copy()
print(new_names) # ['犬夜叉', '火影忍者', '海贼王']
print(id(names), id(new_names)) # 4502035424 4502032864
3.列表.count(元素) - 统计指定元素在列表中出现的次数
nums = [23, 89, 10, 89, 10, 6, 85]
print(nums.count(10)) # 2
print(nums.count(23)) # 1
print(nums.count(100)) # 0
- 列表.extend(序列) - 将序列中所有的元素全部添加到列表中
names = ['犬夜叉', '火影忍者', '海贼王']
names.extend(['死亡笔记', '一拳超人'])
print(names) # ['犬夜叉', '火影忍者', '海贼王', '死亡笔记', '一拳超人']
names = ['犬夜叉', '火影忍者', '海贼王']
names.extend('abc')
print(names) # ['犬夜叉', '火影忍者', '海贼王', 'a', 'b', 'c']
- 列表.index(元素) - 获取指定元素在列表中的下标(返回的是0开始的下标值)
names = ['犬夜叉', '火影忍者', '海贼王', '犬夜叉']
print(names.index('火影忍者')) # 1
如果元素不存在会报错
print(names.index('死神')) # 报错!ValueError: '死神' is not in list
如果元素有多个,只取第一个的下标
print(names.index('犬夜叉')) # 0
6.列表.reverse() - 列表倒序
names = ['犬夜叉', '火影忍者', '海贼王']
names.reverse()
print(names) # ['海贼王', '火影忍者', '犬夜叉']
7.列表.sort() - 将列表中的元素从小到大排序(不会产生新的列表)
列表.sort(reverse=True) - 将列表中的元素从大到小排序(不会产生新的列表)
nums = [23, 89, 10, 89, 10, 6, 85]
re = nums.sort()
print(nums) # [6, 10, 10, 23, 85, 89, 89]
print(re) # None(表示没有)