Home > Software design >  Return a boolean array of values < 40
Return a boolean array of values < 40

Time:05-04

How can I get a boolean 1 dimentional output for values <40 from the below given array. Since there are three values <40 so the output should be: array([ True, True, True])

x = np.array([[40, 37, 70],[62, 61, 98],[65, 89, 22],[95, 98, 81],[44, 32, 79]])

CodePudding user response:

You can do it like this:

import numpy as np
x = np.array([[40, 37, 70],[62, 61, 98],[65, 89, 22],[95, 98, 81],[44, 32, 79]])
x<40

Output:

array([[False,  True, False],
       [False, False, False],
       [False, False,  True],
       [False, False, False],
       [False,  True, False]])

Or if you want a 1d result, you can use .flatten():

y = x.flatten()
y<40

Output:

array([False,  True, False, False, False, False, False, False,  True,
       False, False, False, False,  True, False])

If you want a 1d list like [True]*n where n is the number of values <40, you can do:

np.array([i for i in x.flatten()<40 if i])

Output:

array([True, True, True])

CodePudding user response:

This could be solved in many ways, one could be:

x[x<40]<40
  • Related