+-

如果键具有关联的数组,我会在表中查找.按照设计,我的表.__ getitem __()somtimes返回None而不是KeyError-s.我希望这个值为None,或与w关联的numpy数组.
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))
只有当我使用上面的代码,并且第一次查找是匹配时,我得到:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
如果我使用value = table [w] .any()或table [w.lower()].any(),那么如果它不匹配,我应该碰到:
AttributeError: 'NoneType' object has no attribute 'any'
我一定错过了正确的方法,怎么办?
最佳答案
IIUC这应该工作:
value = table[w]
if value is None:
value = table[w.lower()]
点击查看更多相关文章
转载注明原文:python – 检查变量是否为None或numpy.array - 乐贴网