如何检查给定 NumPy 数组的元素是否非零?
在 NumPy 中,借助any()函数,我们可以检查 NumPy 中给定数组的任何元素是否非零。我们将在 any()函数中传递一个数组,如果它返回 true,那么数组的任何元素都是非零,如果它返回 false,那么数组的所有元素都为零。
Syntax: numpy.any()
Parameter :An array.
Return :It return boolean value(True/False).
示例 1:
Python
import numpy as np
# Original array
array = np.array([4,6,0,0,0,4,89])
print(x)
# Test whether any of the elements
# of a given array is non-zero
print(np.any(array))
Python
import numpy as np
# Original array
array = np.array([0,0,0,0,0,0])
print(x)
# Test whether any of the elements
# of a given array is non-zero
print(np.any(array))
输出:
True
示例 2:
Python
import numpy as np
# Original array
array = np.array([0,0,0,0,0,0])
print(x)
# Test whether any of the elements
# of a given array is non-zero
print(np.any(array))
输出:
False