如何检查numpy数组是否为空?

时间:2021-09-11 21:25:21

How can I check whether the numpy array is empty or not?

如何检查numpy数组是否为空?

I used the following code, but this is fail if the array contains a zero.

我使用了以下代码,但如果数组包含零,则会失败。

if not self.Definition.all():

is this the solution?

这是解决方案吗?

if self.Definition == array( [] ):

2 个解决方案

#1


153  

You can always take a look at the .size attribute:

您始终可以查看.size属性:

import numpy as np
a = np.array([])
print a.size # 0

#2


8  

http://www.scipy.org/Tentative_NumPy_Tutorial#head-6a1bc005bd80e1b19f812e1e64e0d25d50f99fe2

http://www.scipy.org/Tentative_NumPy_Tutorial#head-6a1bc005bd80e1b19f812e1e64e0d25d50f99fe2

NumPy's main object is the homogeneous multidimensional array. In Numpy dimensions are called axes. The number of axes is rank. Numpy's array class is called ndarray. It is also known by the alias array. The more important attributes of an ndarray object are:

NumPy的主要对象是同构多维数组。在Numpy中,尺寸称为轴。轴数是等级。 Numpy的数组类称为ndarray。它也被别名数组所知。 ndarray对象的更重要的属性是:

ndarray.ndim
the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank.

ndarray.ndim数组的轴数(维度)。在Python世界中,维度的数量称为排名。

ndarray.shape
the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the rank, or number of dimensions, ndim.

ndarray.shape数组的维度。这是一个整数元组,表示每个维度中数组的大小。对于具有n行和m列的矩阵,形状将为(n,m)。因此,形状元组的长度是等级或维数ndim。

ndarray.size
the total number of elements of the array. This is equal to the product of the elements of shape.

ndarray.size数组元素的总数。这等于形状元素的乘积。

#1


153  

You can always take a look at the .size attribute:

您始终可以查看.size属性:

import numpy as np
a = np.array([])
print a.size # 0

#2


8  

http://www.scipy.org/Tentative_NumPy_Tutorial#head-6a1bc005bd80e1b19f812e1e64e0d25d50f99fe2

http://www.scipy.org/Tentative_NumPy_Tutorial#head-6a1bc005bd80e1b19f812e1e64e0d25d50f99fe2

NumPy's main object is the homogeneous multidimensional array. In Numpy dimensions are called axes. The number of axes is rank. Numpy's array class is called ndarray. It is also known by the alias array. The more important attributes of an ndarray object are:

NumPy的主要对象是同构多维数组。在Numpy中,尺寸称为轴。轴数是等级。 Numpy的数组类称为ndarray。它也被别名数组所知。 ndarray对象的更重要的属性是:

ndarray.ndim
the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank.

ndarray.ndim数组的轴数(维度)。在Python世界中,维度的数量称为排名。

ndarray.shape
the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the rank, or number of dimensions, ndim.

ndarray.shape数组的维度。这是一个整数元组,表示每个维度中数组的大小。对于具有n行和m列的矩阵,形状将为(n,m)。因此,形状元组的长度是等级或维数ndim。

ndarray.size
the total number of elements of the array. This is equal to the product of the elements of shape.

ndarray.size数组元素的总数。这等于形状元素的乘积。