Python comes with all(), any() with numpy to determine the empty matrix and the zero matrix None, and shape

Essentially, any()the OR operation is all()realized , and the AND operation is realized.

any(iterables) and all(iterables) are very useful for checking the equality of two objects, but it should be noted that any and all are built-in Python functions, and numpy also has its own implementation of any and all, which are the same as built-in Python, except Added the numpy.ndarray type. Because python's built-in ndarray cannot be understood with more than one dimension, the calculations based on numpy are best to use any and all implemented by numpy itself


Reference: https://blog.csdn.net/cython22/article/details/78829288

First use the empty matrix processing:

>>> import numpy as np
>>> a=np.array([])
>>> any(a)
False
>>> all(a)
True
>>> np.any(a)
False
>>> np.all(a)
True

Zero matrix processing in various situations:

>>> b=np.array([0])
>>> any(b)
False
>>> all(b)
False
>>> np.any(b)
False
>>> np.all(b)
False
>>> c=np.array([[0,0]])
>>> any(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous.
 Use a.any() or a.all()
>>> c.any()
False
>>> c.all()
False
>>> np.any(c)
False
>>> np.all(c)
False

About None return:

>>> a==None
array([], dtype=bool)

>>> b==None
array([False])

>>> c==None
array([[False, False]])

About shape return is used to judge the most reliable.

Guess you like

Origin blog.csdn.net/qq_36401512/article/details/105533919