一、可变/不可变类型二、有/无序类型三、运算符 和 函数循环过程中删除可变类型元素引发的bug
一、可变/不可变类型# 删除33后,44占到33的位置上去了 data = [11, 22, 33, 44, 55, 66, 77] for i in data: if i == 33 or i == 44: data.remove(i) print(data) # 解决方法:把想删除的值放入临时列表,遍历临时列表对data进行删值 data = [11, 22, 33, 44, 55, 66, 77] list_temp = [] for i in data: if i == 33 or i == 44: list_temp.append(i) print(list_temp) for i in list_temp: if i == 33 or i == 44: data.remove(i) # [11,22,55,66,77] print(data)
[11, 22, 44, 55, 66, 77] [33, 44] [11, 22, 55, 66, 77]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)