num =97
scores =[98,93,91,87,83,80,77,70,65,62,56]for index inrange(len(scores)):if scores[index]< num:
scores.insert(index, num)breakelse:
scores.append(num)print(scores)"""
num = 97
scores = [98, 93, 91, 87, 83, 80, 77, 70, 65, 62, 56]
index -> range(11)
index = 0: if scores[0] < 97 -> if 98 < 97
index = 1: if scores[1] < 97 -> if 93 < 97 -> scores.insert(1, 97) -> scores = [98, 97,93, 91, 87, 83, 80, 77, 70, 65, 62, 56] ->break
循环结束
print(scores) -> [98, 97,93, 91, 87, 83, 80, 77, 70, 65, 62, 56]
"""
2. 删 - 删除列表中的元素
1) del 列表[下标] - 删除列表中指定下标对应的元素
tvs =['回家的诱惑','非自然死亡','我的兄弟叫顺溜','琅琊榜','甄嬛传','亮剑','请回答1988']del tvs[1]print(tvs)# ['回家的诱惑', '我的兄弟叫顺溜', '琅琊榜', '甄嬛传', '亮剑', '请回答1988']del tvs[-1]print(tvs)# ['回家的诱惑', '我的兄弟叫顺溜', '琅琊榜', '甄嬛传', '亮剑']# 注意:下标不能越界# del tvs[6] # IndexError: list assignment index out of range
2)列表.remove(元素) - 删除列表中指定元素
如果需要删除的元素在列表中有多个,只删最前面那一个
如果需要删除的元素不存在,程序会报错
tvs =['回家的诱惑','非自然死亡','我的兄弟叫顺溜','琅琊榜','甄嬛传','亮剑','琅琊榜','请回答1988','琅琊榜']
tvs.remove('非自然死亡')print(tvs)# ['回家的诱惑', '我的兄弟叫顺溜', '琅琊榜', '甄嬛传', '亮剑', '琅琊榜', '请回答1988', '琅琊榜']
tvs.remove('琅琊榜')print(tvs)# ['回家的诱惑', '我的兄弟叫顺溜', '甄嬛传', '亮剑', '琅琊榜', '请回答1988', '琅琊榜']# tvs.remove('吸血鬼日记') # ValueError: list.remove(x): x not in list