为何遍历a只遍历3次?智商不够了,list里最后一个为何没遍历到?智商不够了。。。
a = ["asd_1","asd_2","3","4"]
b = a
for i in a:
print(i)
if i.find('asd_') < 0:
b.remove(i)
输出:
asd_1
asd_2
3
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
因为列表上可变对象啊,a和b其实只想的是同一个地址,在b上面remove会影响a的迭代,不信你把a打印出来看
输出:
这时a的长度已经变成3了
上面的代码,b只是a的引用,你修改了b,a也被修改了,直接影响了a的迭代.
你可以试试
或者