Issue
What's the difference between the following:
a = np.array([2,3,4])
b = np.array([2,7,8])
if a.any() == b.all():
print('yes')
and
a = np.array([2,3,4])
b = np.array([2,7,8])
if a.any() == b.any():
print('yes')
In both situations, 'yes'
is printed.
Solution
any()
and all()
are intended for boolean arrays. any()
returns True
if there's any values that are equal to True
in the array. all()
returns True
if all values in the array are equal to True
.
For integers/floats the functionality is similar, except that they return True
if the value 0
is not found in the array.
In your example, since both a.any()
and a.all()
will return True
, it follows that a.any() == a.all()
.
Try executing the following code to see how it works in practice.
a = np.asarray([1,2,3])
b = np.asarray([-1,0,1])
c = np.asarray([True, False])
print(a.any())
print(a.all())
print(b.any())
print(b.all())
print(c.any())
print(c.all())
Answered By - user2653663
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.