w3resource

NumPy: Fetch all items from a given array of 4,5 shape which are either greater than 6 and a multiple of 3

NumPy: Array Object Exercise-179 with Solution

Write a NumPy program to fetch all items from a given array of 4,5 shape which are either greater than 6 and a multiple of 3.

Sample Solution:

Python Code:

import numpy as np
array_nums1 = np.arange(20).reshape(4,5)
print("Original arrays:")
print(array_nums1)
result = array_nums1[(array_nums1>6) & (array_nums1%3==0)]
print("\nItems greater than 6 and a multiple of 3 of the said array:")
print(result)

Sample Output:

Original arrays:
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]

Items greater than 6 and a multiple of 3 of the said array:
[ 9 12 15 18]

Python Code Editor:

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

Previous: Write a NumPy program to replace all the nan (missing values) of a given array with the mean of another array.
Next: Write a NumPy program to check whether the dimensions of two given arrays are same or not.

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}