python - 在浮动列表中查找浮动列表的索引
问题描述
我有一个使用这些局部最大值的索引返回所有局部最大值的函数。在我得到每个高于平均值的局部最大值之后,我想从 game_log 列表中返回这些局部最大值的索引。本质上,我想[38.2,34.5,34.5]
从 game_log 列表中返回索引。有没有办法用浮点数列表搜索浮点数列表并获取列表中的相应索引?
import numpy as np
local_max = [ 3, 6, 8, 10]
game_log = [22.7, 16.7, 18.5, 38.2, 1.5, 8.6,
12.6, 7.4, 34.5, 23.2, 34.5, 20.5, 24.0, 35.1]
average_points = sum(game_log) / len(game_log)
def filter_local_max_under_avg(game_log, local_max_list, avg):
res_list = [game_log[i] for i in local_max_list]
filtered_list = [i for i in res_list if i >= avg]
return filtered_list
vals = filter_local_max_under_avg(game_log, local_max, average_points)
print(vals)
解决方案
假设您可以继续使用返回局部最大值索引的函数,听起来您只需要第二个函数来检查它们是否高于平均值。就像是:
def filter_local_max_under_avg(game_log, local_max, average_points):
res = list()
for idx in local_max:
if game_log[idx] > average_points:
res.append(idx)
return res
或作为发电机...
def filter_local_max_under_avg(game_log, local_max, average_points):
for idx in local_max:
if game_log[idx] > average_points:
yield idx
推荐阅读
- openiddict - 有没有办法在 OpenIddict 中动态加载声明?
- html - CSS/Masonry -> 跨越多列
- asp.net-core - 模态局部视图 Asp .Net Core 3 中没有验证
- r - 加载 R 包时出错 - libicui18n.so.68:无法打开共享对象文件
- google-calendar-api - 我正在尝试使用 Google Calendar API 读取用户日历事件。在参加者对象中,我只看到少数参与者的“显示名称”
- python - 字符串到字符串和整数列表
- python - 在初始化期间动态创建属性,而无需事先知道属性的数量或名称
- c++ - 无法导入 C++ 模块
- php - php减少多数组的值
- python - 在 beautifulsoup4 中,当纯粹基于元素和其中的文本抓取网站时,如何返回多个结果?