a=[1,2,1,4,1,]
n=a[0]
while n<len(a)+1:
if a[n]==1:
a.remove(a[n])
n=n+1
print (a)
这样写执行的结果列表最后的1删不掉
a=[1,2,1,4,1,]
n=a[0]
while n<len(a)+1:
if a[n]==1:
del a[n]
n=n+1
print (a)
判断条件下的删除改成del,最前面的1删不掉。
a=[1,2,1,4,1,]
n=a[0]
while n<len(a)+1:
if a[n]==1:
a.pop(n)
n=n+1
print (a)
把判断条件下的删除改成pop,最前面的1也是删不掉
a=[1,2,1,4,1,]
for i in a:
if i==1:
a.remove(1)
print (a)
改成for循环,又没问题
现在有个问题,就是删除重复值1,for循环配合del,pop,remove怎么写,while循环配合del,pop,remove代码又该怎么写。可能我的问题有些复杂。请原谅我是小白。希望大神不吝赐教。谢谢