I came across a function named any with numpy
and I could not understand its usage in some context which is given as folllows:
if np.subtract(original.shape, duplicate.shape).any():
# Do something
else:
# Carry on with the usual tasks
Could someone help me understand what is happening here? What is being checked? The documentation says,
Tests whether any array element along a given axis evaluates to True.
Is it being checked for equality? To understand this better, how could I rewrite the any
call?
It's being checked for "True"ness.
Try this:
import numpy
print(numpy.any([0, 0, 0, 0, 0]))
print(numpy.any([0, 0, 0, 0, 1]))
np.any(x)
checks if any of the elements in x
is true. In your case, it checks if the arrays original
and duplicate
have at least a different dimension.
You could rewrite this as:
res = False
for so, sd in zip(original.shape, duplicate.shape):
if so != sd:
res = True
if res:
# Do something
else:
# Carry on with the usual tasks
The any
method checks if at least on element in the given data is evaluated as True
.
In python the following things are evaluated False
:
None
False
__len__
method which returns 0 or a __bool__
method that returns False
Everything else is evaluated True
.
If the data checked by the any
method contains at least one item that does not meet these requirements, it returns True
else False
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.