我有一个函数,它使用这些本地maxes的索引返回所有的局部最大值。在得到高于平均值的每个局部最大值之后,我想从game_log列表中返回这些本地max的索引。实际上,我希望从[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)发布于 2019-11-22 00:44:40
假设您可以继续使用返回本地maxes索引的函数,听起来您只需要第二个函数来检查它们是否高于平均值。类似于:
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发布于 2019-11-22 00:52:25
下面是一个通用函数,用于根据另一个列表搜索一个列表,并返回匹配索引的列表
def get_matches(target: list, query: list) -> list:
result = []
for x in query:
try:
temp = target.index(x)
result.append(temp)
except ValueError: #thrown if no match found
pass
return result 发布于 2019-11-22 00:39:24
这段代码将为您提供浮点数的索引,然后可以在循环中从它创建一个列表。您将不得不使用try/除非您有不在索引中的浮点数。
game_log.index(18.5)
# 2https://stackoverflow.com/questions/58985801
复制相似问题