<p>我在表中查找键是否有关联数组。通过设计,my<code>table.__getitem__()</code>somtimes返回<code>None</code>,而不是<code>KeyError</code>-s。我希望此值是<code>None</code>,或者是与<code>w</code>关联的numpy数组。</p>
<pre><code>value = table[w] or table[w.lower()]
# value should be a numpy array, or None
if value is not None:
stack = np.vstack((stack, value))
</code></pre>
<p>只有当我使用上面的代码,并且第一次查找是匹配的,我才能得到:</p>
<pre><code>ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
</code></pre>
<p>如果我使用<code>value = table[w].any() or table[w.lower()].any()</code>,那么如果它不匹配,我会遇到:</p>
<pre><code>AttributeError: 'NoneType' object has no attribute 'any'
</code></pre>
<p>我一定是错过了正确的方法,怎么办?</p>