I have following array:
scores=[2.619,3.3, 9.67, 0.1, 6.7,3.2]
And I wish to retrieve the elements which more than 5 by following code:
min_score_thresh=5
Result=scores[scores>min_score_thresh]
Hence this will result me result:
[9.67, 6.7]
Now i wish to get the positions of this two element which is my expected answer will be store in variable x:
x = [2,4]
Please share me ideas, thanks
Use numpy.where
for a vectorised solution:
import numpy as np
scores = np.array([2.619,3.3, 9.67, 0.1, 6.7,3.2])
min_score_thresh = 5
res = np.where(scores>min_score_thresh)[0]
print(res)
[2 4]
scores = [2.619, 3.3, 9.67, 0.1, 6.7, 3.2]
min_score_thresh = 5
# score is 5 or higher
result = []
# position in 'scores' list
indx = []
for i, item in enumerate(scores):
if item > min_score_thresh:
result.append(item)
indx.append(i)
x = indx
print(result)
print(x)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With