I have a list of named tuples. Each named tuple is a DataPoint
type I have created, that looks like this:
class DataPoint(NamedTuple):
data: float
location_zone: float
analysis_date: datetime
error: float
At various points throughout my code, I have to get all the DataPoints
in the list by a particular attribute. Here's how I do it for analysis_date
, I have similar functions for the other attributes:
def get_data_points_on_date(self, data_points, analysis_date):
data_on_date = []
for data_point in data_points:
if data_point.analysis_date == analysis_date:
data_on_date.append(data_point)
return data_on_date
This is called >100,000 times on lists with thousands of points, so it is slowing down my script significantly.
Instead of a list, I could do a dictionary for a significant speedup, but because I need to search on multiple attributes, there isn't an obvious key. I would probably choose the function that is taking up the most time (in this case, analysis_date
), and use that as the key. However, this would add significant complexity to my code. Is there anything besides hashing / a clever way to hash that is escaping me?