擅长:python、mysql、java
<p>以下函数将在条件为<code>True</code>的情况下删除<strong>所有项:</p>
<pre><code>def remove(list,condtion):
ii = 0
while ii < len(list):
if condtion(list[ii]):
list.pop(ii)
continue
ii += 1
</code></pre>
<p>以下是如何使用它:</p>
<pre><code>class Thing:
def __init__(self,ch,ii):
self.ch = ch
self.ii = ii
def __repr__(self):
return '({0},{1})'.format(self.ch,self.ii)
things = [ Thing('a',0), Thing('b',0) , Thing('a',1), Thing('b',1)]
print('Before ==> {0}'.format(things)) # Before ==> [(a,0), (b,0), (a,1), (b,1)]
remove( things , lambda item : item.ch == 'b')
print('After ==> {0}'.format(things)) # After ==> [(a,0), (a,1)]
</code></pre>