定义一个列表保存多个学生的分数
,删除列表中所以低于60分的值
scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66]
for x in scores:
if x<60:
scores.remove(x)
print(scores)
结果:[60, 89, 12, 99, 80, 71, 66]
少了个12没删,这是为什么
定义一个列表保存多个学生的分数
,删除列表中所以低于60分的值
scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66]
for x in scores:
if x<60:
scores.remove(x)
print(scores)
结果:[60, 89, 12, 99, 80, 71, 66]
少了个12没删,这是为什么
不能一边遍历一边删。
可以用列表解析式:
scores=[45,60,89,30,12,59,99,80,71,66]
scores=[s for s in scores if s>=60]
print(scores)