How to use numpy.where() to get elements from two arrays respectively depend on condition in Python

2 Answers

0 votes
import numpy as np

arr = np.array([45, 13, 18])

result = np.where(arr < 20, [1, 2, 3], [4, 5, 6])
 
print(result)
  
 
  
'''
run:
 
[4 2 3]

'''

 



answered Feb 6, 2020 by avibootz
0 votes
import numpy as np

arr = np.array([3, 30, 18])

result = np.where(arr > 19, [1, 2, 3], [4, 5, 6])
 
print(result)
  
 
  
'''
run:
 
[4 2 6]

'''

 



answered Feb 6, 2020 by avibootz
...