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.
JavaScript
2
1
if not self.Definition.all():
2
Is this the solution?
JavaScript
2
1
if self.Definition == array([]):
2
Advertisement
Answer
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:
JavaScript
6
1
import numpy as np
2
a = np.array([])
3
4
if a.size == 0:
5
# Do something when `a` is empty
6