Skip to content

Instantly share code, notes, and snippets.

@mbforbes
Created August 8, 2014 01:54
Show Gist options
  • Save mbforbes/1f6db4bf8ceccb9d2388 to your computer and use it in GitHub Desktop.
Save mbforbes/1f6db4bf8ceccb9d2388 to your computer and use it in GitHub Desktop.
Get top w.r.t. attr
@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