考慮: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從列表中刪除?請(qǐng)注意,我僅以整數(shù)為例,在實(shí)際應(yīng)用程序中有一個(gè)任意對(duì)象列表。謝謝。
3 回答

瀟湘沐
TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
遍歷列表的淺表副本。
由于您無法在迭代時(shí)修改列表,因此您需要迭代列表的淺表副本。
fooList = [1, 2, 3, 4]
for foo in fooList[:]: #equivalent to list(fooList), but much faster
if fooChecker(foo):
fooList.remove(foo)
添加回答
舉報(bào)
0/150
提交
取消