如何检查 numpy 数组是否为空? [英] How can I check whether a numpy array is empty or not?
本文介绍了如何检查 numpy 数组是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查 numpy 数组是否为空?
How can I check whether a numpy array is empty or not?
我使用了以下代码,但如果数组包含零,则失败.
I used the following code, but this fails if the array contains a zero.
if not self.Definition.all():
这是解决方案吗?
if self.Definition == array( [] ):
推荐答案
您可以随时查看 .size
属性.它被定义为整数,并且是当数组中没有元素时为零(0
):
You can always take a look at the .size
attribute. It is defined as an integer, and is zero (0
) when there are no elements in the array:
import numpy as np
a = np.array([])
if a.size == 0:
# Do something when `a` is empty
这篇关于如何检查 numpy 数组是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文