擅长:python、mysql、java
<p>您可以使用<a href="https://docs.python.org/3.5/library/stdtypes.html#dict.get" rel="nofollow noreferrer">^{<cd1>}</a>来缩短代码。不是在键不存在时引发<code>KeyError</code>,而是返回<code>None</code>:</p>
<pre><code>foo = data.get('foo')
bar = data.get('email')
if foo is not None and bar is not None:
dofoobar()
elif foo is not None:
dofoo()
elif bar is not None:
dobar()
</code></pre>