Created
August 8, 2014 01:54
-
-
Save mbforbes/1f6db4bf8ceccb9d2388 to your computer and use it in GitHub Desktop.
Get top w.r.t. attr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@staticmethod | |
def get_top_wrt_attr(objs, attr, epsilon): | |
''' | |
Gets the top elements from objs that score within epsilon of | |
the max value with respect to their (number-valued) attribute | |
attr. | |
Args: | |
objs ([object]) | |
attr (str) | |
epsilon (float) | |
Returns: | |
[object]: A subset of objs. | |
''' | |
max_score = max([getattr(o, attr) for o in objs]) | |
return [o for o in objs if max_score - getattr(o, attr) <= epsilon] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment