Reputation: 747
I have a function that returns all local maximum using the index of these local maxes. After I get each local max that is above the average I would then like to return the index of those local maxes from the game_log list. Essentially, I want to return the index of [38.2,34.5,34.5]
from the game_log list. Is there a way to search a list of floats with a list of floats and get the corresponding indexes in a list?
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)
Upvotes: 0
Views: 1336
Reputation: 1299
Here's a general function for searching one list against another and returning a list of the matching indexes
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
Upvotes: 1
Reputation: 983
Assuming you are okay with continuing to use the function which returns the indexes of the local maxes, it sounds like you just need a second function which checks if they are above the average. Something like:
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
or as a generator...
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
Upvotes: 2
Reputation: 7224
This code will give you the index of the float, you can then create a list from it in a loop. You'll have to use try/except if you have floats that aren't in the index.
game_log.index(18.5)
# 2
Upvotes: 0