考虑:fooList = [1, 2, 3, 4] # Ints for example only, in real application using objectsfor foo in fooList: if fooChecker(foo): remove_this_foo_from_list具体如何foo从列表中删除?请注意,我仅以整数为例,在实际应用程序中有一个任意对象列表。谢谢。
3 回答
潇湘沐
TA贡献1816条经验 获得超6个赞
遍历列表的浅表副本。
由于您无法在迭代时修改列表,因此您需要迭代列表的浅表副本。
fooList = [1, 2, 3, 4]
for foo in fooList[:]: #equivalent to list(fooList), but much faster
if fooChecker(foo):
fooList.remove(foo)
添加回答
举报
0/150
提交
取消
