擅长:python、mysql、java
<pre><code>def f(lst):
flt = filter(lambda x : x is not None and x != 0, lst)
try:
return min(flt)
except ValueError:
return None
</code></pre>
<p>当序列为空时,<code>min</code>抛出<code>ValueError</code>。这遵循了常见的“更容易请求原谅”的模式。</p>
<p>EDIT:基于reduce的解决方案</p>
<pre><code>from functools import reduce
def f(lst):
flt = filter(lambda x : x is not None and x != 0, lst)
m = next(flt, None)
if m is None:
return None
return reduce(min, flt, m)
</code></pre>