擅长:python、mysql、java
<p>python中的空值是<code>None</code>。所以可能是这样的:</p>
<pre><code>a1 = None
a2 = 5
a3 = None
a = [a1,a2,a3]
def get_value(L):
return [x for x in L if x is not None][0]
a = get_value(a)
print(a)
</code></pre>
<p>输出:</p>
<pre><code>5
</code></pre>
<p><strong>更新</strong>:如果列表<code>a</code>中没有值,则此操作可能会失败,因此我们应该为这种情况包括异常处理:</p>
<pre><code>def get_value(L):
try:
return [x for x in L if x is not None][0]
except IndexError:
return None
</code></pre>