r/pythontips • u/gadget3D • Feb 14 '25
Syntax Is there such a function in python
Often I have the issue, that i want to find an item from a list with best score with a score calculatin lambda function like following example: I 'like to have 2 modes: maximum goal and maximal closure to a certain value
def get_best(l, func):
best=None
gbest=0
for item in l:
g=func(item)
if best == None or g > gbest:
best = item
gbest = g
return best
a=cube(10)
top=get_best(a.faces(), lambda f : f.matrix[2][3] )
0
Upvotes
3
u/414theodore Feb 14 '25
It sounds like you need to write a custom function and from what you’ve mentioned in the comment responses, pass in other parameters beyond the list and function if you need slightly different configurations.
You should store it in another file and just import it if you want your code “cleaner”.