擅长:python、mysql、java
<p>您可以创建一个<code>catch</code>对象</p>
<pre><code>def catch(error, default, function, *args, **kwargs):
try: return function(*args, **kwargs)
except error: return default
</code></pre>
<p>那你就可以了</p>
<pre><code># using None as default value
result (catch(Exception, None, plot_pdf, f) for f in file_list)
</code></pre>
<p>然后你就可以做你想做的事情了:</p>
<pre><code>result = list(result) # turn it into a list
# or
result = [n for n in result if n is not None] # filter out the Nones
</code></pre>
<p>不幸的是,这甚至不是远程C速度,<a href="https://stackoverflow.com/questions/28816432/python-what-is-the-fastest-way-to-map-or-compress-calls-and-ignore-errors?noredirect=1#comment45904633_28816432">see my question here</a></p>