首页 > 解决方案 > 在浮动列表中查找浮动列表的索引

问题描述

我有一个使用这些局部最大值的索引返回所有局部最大值的函数。在我得到每个高于平均值的局部最大值之后,我想从 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)

标签: pythonlist

解决方案


假设您可以继续使用返回局部最大值索引的函数,听起来您只需要第二个函数来检查它们是否高于平均值。就像是:

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

推荐阅读