A
A
albus-feles2020-06-11 22:58:18
Python
albus-feles, 2020-06-11 22:58:18

How to check array elements for some condition?

There is a random array: Using this array, you need to transform it into a new array with the following values:

а = np.random.randint(15, 37, (2, 3, 4))


  • "small" if values ​​are less than 20
  • "medium" if the values ​​are between [20, 30]
  • "large" if values ​​are greater than 30

For example we have an array:

[

It is necessary to carry out the elements under these conditions. The output is something like this array:

[

I don’t understand how to pass a numeric element under a condition and get a value of type str instead.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2020-06-11
@fox_12

a = np.random.randint(15, 37, (2, 3, 4))
print(a)

[

 ]


b = np.full_like(a.astype(str), 'medium')

b[a < 20] = 'small'
b[a > 30] = 'large'

print(b)
[

 ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question