w3resource

NumPy: Test whether any array element along a given axis evaluates to True

NumPy: Array Object Exercise-24 with Solution

Write a NumPy program to test whether any array element along a given axis evaluates to True.
Note: 0 evaluates to False in python.

Pictorial Presentation:

Python NumPy: Test whether any array element along a given axis evaluates to True

Sample Solution:-

Python Code:

import numpy as np
print(np.any([[False,False],[False,False]]))
print(np.any([[True,True],[True,True]]))
print(np.any([10, 20, 0, -50]))
print(np.any([10, 20, -50]))

Sample Output:

False                                                                  
True                                                                   
True                                                                   
True

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to test if all elements in an array evaluate to True.
Next: Write a NumPy program to construct an array by repeating.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}