Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can use the numpy function np.count_nonzero to count the number of non-zero elements in an array. If the count is 0, then the array consists exclusively of zeros.

Here's an example:

import numpy as np # Example array a = np.zeros((3, 4)) # Check if array exclusively consists of zeros if np.count_nonzero(a) == 0: print("Array contains only zeros") else: print("Array contains at least one non-zero element") 

Output:

Array contains only zeros