python中最好不要在list遍历中使用list.remove方法:
remove 仅仅 删除一个值的首次出现。
如果在 list 中没有找到值,程序会抛出一个异常
最后,你遍历自己时候对自己的内容进行删除操作,效率显然不高,还容易出现各种难debug的问题
建议使用新的list存储要保留的内容,然后返回这个新list。比如
a_list = [1,2,3,4,5]needs_to_be_removed = [3,4,5]
result = []
for v in a_list:
if v not in needs_to_be_removed:
result.append(v)
print result
不能这么删for user in users:这种循环体不要去对users做操作
应该这样
for i in range(len(users)):users.remove(users[i])
users2.append(users[i])