python100day - day5 列表
1.列表删除
练习:删除指定分数列表中所有低于60分的成绩
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
# 坑一:直接遍历用remove删除元素 - 删不干净(因为遍历的时候没有把所有元素都遍历出来)
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
for x in scores:
print('x', x)
if x < 60:
scores.remove(x) # [98, 34, 89, 67, 9, 100, 78]
print(scores)
'''
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
x = 98: 98<60 -> False
x = 45: 45<60 -> True -> scores.remove(45) -> scores = [98, 34, 89, 23, 67, 23, 9, 54, 100, 78]
x = 89: 89<60 -> False
x = 23: 23<60 -> True -> scores.remove(23) -> scores = [98, 34, 89, 67, 23, 9, 54, 100, 78]
x = 23: 23<60 -> True -> scores.remove(23) -> scores = [98, 34, 89, 67, 9, 54, 100, 78]
x = 54: 54<60 -> True -> scores.remove(54) -> scores = [98, 34, 89, 67, 9, 100, 78]
x = 78: 78<60 -> False
print(scores)
'''
# 解决坑一:创建一个和原列表一样的新列表,遍历新列表,删除原列表
scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
# scores1 = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
for x in scores.copy(): # 复制一个新的列表
if x < 60:
scores.remove(x)
print(scores)
for x in scores+[]:
if x < 60:
scores.remove(x)
print(scores)
for x in scores[:]:
if x < 60:
scores.remove(x)
print(scores)
for x in scores*1:
if x < 60:
scores.remove(x)
print(scores)
# 坑二:报错!(下标越界)
# scores = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
len(列表) - 获取列表中元素的个数
for index in range(len(scores)):
if index < 60:
del scores[index] # IndexError: list assignment index out of range
print(scores)
# 解决坑二:
print('=======解决坑二=======')
scores1 = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
index = 0
while index < len(scores): # while index < len(scores):
s = scores1[index]
if s < 60:
del scores1[index]
else:
index += 1
if index >= len(scores1):
break
print(scores1)
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 = ['苹果', '香蕉', '猕猴桃', '橘子', '石榴', '葡萄']
'''
2)省略开始下标
列表[:结束下标:步长]
步长为正:从第一个元素开始往后取
步长为负:从最后一个元素开始往前取(相当于开始下标是-1)
'''
fruits = ['苹果', '香蕉', '猕猴桃', '橘子', '石榴', '葡萄']
print(fruits[:4]) # ['苹果', '香蕉', '猕猴桃', '橘子']
print(fruits[: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[:]) # ['苹果', '香蕉', '猕猴桃', '橘子', '石榴', '葡萄']
3.列表的相关操作
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 = list3 * 2
print(list4) # [10, 20, 30, 100, 200, 10, 20, 30, 100, 200]
list5 = list1 * 1
print(id(list1), id(list5)) # 2046696575496 2046698531720
2.比较运算
# 两个列表之间支持比较大小和相等
# 1)两个列表比较大小 - 比较第一对不相等元素的大小,谁大对应的列表就大
list11 = [1, 2, 3, 4, 5]
list22 = [10, 20]
print(list11 > list22) # False
print(list22 > list11) # True
# print([1, '2', 3] > [1, 2, 3])
# 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
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([1, 2] in [1, 2, 3, 4]) # False
print([1, 2] in [[1, 2], 3, 4]) # True
# 练习:获取两个列表中公共的元素
# A = [1, 2, 5, 10, 3, 2]
# B = [5, 2, 10, 20, 32]
# 求公共列表C:[]
A = [1, 2, 5, 10, 3, 2]
B = [5, 2, 10, 20, 32]
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、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(sorted(new_scores)) # [34, 60, 65, 78, 78, 89, 90]
new_scores1 = sorted(scores, reverse=True)
print(new_scores1) # [90, 89, 78, 78, 65, 60, 34]
# 不会修改原列表
print(scores)
# 4).len(列表) - 获取列表长度(列表中元素的个数)
print(len(scores)) # 7
# 5)list(数据) - 将指定数据转换成列表(数据必须是序列:转换的时候直接将序列中的元素作为新的元素)
print(list('abc')) # ['a', 'b', 'c']
print(list(range(4))) # [0, 1, 2, 3]
4.列表相关的方法
1.列表.clear() - 清空指定列表
names = ['犬夜叉', '火影忍者', '海贼王']
names.clear()
print(names) # []
2.列表.copy() - 复制指定列表产生一个一模一样的新列表(地址不同)
names = ['犬夜叉', '火影忍者', '海贼王']
new_names = names.copy()
print(new_names) # ['犬夜叉', '火影忍者', '海贼王']
print(id(names), id(new_names)) # 2054650549256 2054650548744
3.列表.count(元素) - 统计指定元素在列表中出现的次数
nums = [23, 89, 10, 89, 10, 6, 85]
print(nums.count(10))
print(nums.count(23))
print(nums.count(6))
print(nums.count(89))
print(nums.count(111))
4.列表.extend(序列) - 将序列中所有的元素全部添加到列表中
names = ['犬夜叉', '火影忍者', '海贼王']
names.extend(['死亡笔记', '一拳超人'])
print(names)
names = ['犬夜叉', '火影忍者', '海贼王'] # ['犬夜叉', '火影忍者', '海贼王', '死亡笔记', '一拳超人']
names.extend('abc')
print(names) # ['犬夜叉', '火影忍者', '海贼王', 'a', 'b', 'c']
5.列表.index(元素) - 获取指定元素在列表中的下标(返回的是0开始的下标值)
names = ['犬夜叉', '火影忍者', '海贼王']
print(names.index('火影忍者')) # 1
# 如果元素不存在会报错
# names = ['犬夜叉', '火影忍者', '海贼王']
# print(names.index('死神')) # ValueError: '死神' is not in list
# 如果元素有多个,只取第一个下标
names = ['犬夜叉', '火影忍者', '海贼王', '犬夜叉']
print(names.index('犬夜叉'))
6.列表.reverse() - 列表倒叙
names = ['犬夜叉', '火影忍者', '海贼王']
names.reverse()
print(names) # ['海贼王', '火影忍者', '犬夜叉']
7.a.列表.sort() - 将列表中的元素从小到大排序
b.列表.sort(reverse=True) - 将列表中的元素从小到大排序(不会产生新的列表)
nums = [23, 89, 10, 89, 10, 6, 85]
re = nums.sort()
print(nums)
print(re)
scores1 = [98, 45, 34, 89, 23, 67, 23, 9, 54, 100, 78]
index = 0
while True: # while index < len(scores):
s = scores1[index]
if s < 60:
del scores1[index]
else:
index += 1
break
print(scores1)