如何通过Python找到数组中元素的位置?

use*_*314 4 python arrays numpy python-3.x

我有以下数组:

scores=[2.619,3.3, 9.67, 0.1, 6.7,3.2]
Run Code Online (Sandbox Code Playgroud)

我希望通过以下代码检索超过 5 个的元素:

min_score_thresh=5
Result=scores[scores>min_score_thresh]
Run Code Online (Sandbox Code Playgroud)

因此,这将导致我的结果:

[9.67, 6.7]
Run Code Online (Sandbox Code Playgroud)

现在我希望得到这两个元素的位置,这是我预期的答案将存储在变量 x 中:

x = [2,4]

请分享我的想法,谢谢

jpp*_*jpp 5

使用numpy.where了矢量化的解决方案:

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]
Run Code Online (Sandbox Code Playgroud)