更新时间:2023-12-05 17:33:10
过滤器(或列表理解)是解决之道.如果您想就地执行此操作,则可以执行以下操作:
Filter (or list comprehension) IS the way to go. If you want to do it inplace, something like this would work:
purge = []
for i,object in enumerate(self.list):
if object.mycond()
purge.append(i)
for i in reversed(purge):
del self.list[i]
或者,可以通过理解来创建清除列表,快捷方式如下:
Or alternatively, the purge list can be made with a comprehension, a shortcut version looks like:
for i in reversed([ i for (i,o) in enumerate(self.list) if o.mycond() ]):
del self.list[i]