幸福手机

文章
8
资源
0
加入时间
3年0月28天

python 列表删除重复元素,remove

ls = [1, 1, 2, 3, 4]for i in ls: if i == 1: ls.remove(i)print(ls) # output: [1, 2, 3, 4]原因是remove改变列表长度和元素的位置,第二次进入for循环时,指向ls[1],而此时ls长度已经改变,ls[1]是2而不是指向元素1了 看一下以下例子的输出就明白了ls =...