Reputation: 33
import numpy as np
y = [1.25, 2.59, -4.87, 6.82, -7.98, -11.23]
if(np.any(y) < -10):
print("yes")
I want to check whether in a list any value below or above a particular value exists or not. So, I proceed with np.any(), code is getting compiled but not working as wished, I mean not printing back "yes".
Upvotes: 0
Views: 101
Reputation: 11
y = [1.25, 2.59, -4.87, 6.82, -7.98, -11.23]
for value in y:
if value < -10:
print(f'yes there is {value} less than -10')
Your Output
Upvotes: 1
Reputation: 1205
This should do the job
import numpy as np
y1 = np.array([1.25, 2.59, -4.87, 6.82, -7.98, -11.23])
y2 = np.array([1.25, 2.59, 4.87, 6.82, 7.98, 11.23])
if(np.any(y1 < -10)):
print("yes y1")
else:
print("no y1")
if(np.any(y2 < -10)):
print("yes y2")
else:
print("no y2")
Other option will be to not use numpy module, and just to scan the array elements one by one.
Upvotes: 0
Reputation: 53
np.any()
returns a bool and you cannot compare it with an int.
it's like doing true/false < -10
The correct use of np.any() would be as follows:
(y < -10).any()
Upvotes: 1
Reputation: 2525
any
should be after the brackets, and y
should be a numpy
array.
import numpy as np
y = np.array([1.25, 2.59, -4.87, 6.82, -7.98, -11.23])
if (y < -10).any():
print("yes")
Upvotes: 6