gpt4 book ai didi

python - Python如何查找数组中元素的位置?

转载 作者:太空宇宙 更新时间:2023-11-03 12:26:26 26 4
gpt4 key购买 nike

我有以下数组:

scores=[2.619,3.3, 9.67, 0.1, 6.7,3.2]

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

min_score_thresh=5
Result=scores[scores>min_score_thresh]

因此这将导致我的结果:

[9.67, 6.7]

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

x = [2,4]

请分享我的想法,谢谢

最佳答案

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]

关于python - Python如何查找数组中元素的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50264196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com