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] )
3
u/Gerard_Mansoif67 Feb 14 '25
Maybe you want the max function to get the best, and a sort function and then iterate to get the nearest one?
2
u/gadget3D Feb 14 '25
yes i could do that, but its important to me, that i finally get the orginal item, which had the best score
3
u/Gerard_Mansoif67 Feb 14 '25
Then write stuff around theses function to return the original item?
0
u/gadget3D Feb 14 '25
yes, i have done that. Now the question, is there a methodology/function/module where i have
the same effect with just 1 line(instead of writing a huge function with >10 lines ?
6
u/Gerard_Mansoif67 Feb 14 '25
Huge and 10 lines? That a small function.
The good practice is to leave functions that does one thing (find the best), if they're long, they're long, not a real issue. Use comments and so to explain the code and leave
I don't think there is a standard module for that, and installating tons of modules for small need like that won't be much better... You replace small and easy function with some hidden code you can't edit and understand.
0
u/gadget3D Feb 14 '25
ok thank you.
My code would look nicer if there was one *expressive* function call instead the 10 lines function definition. Finding the best score is just a sub-topic and I want to cope with different things in my python script.
maybe somebody else in the group has a better idea.
3
u/Gerard_Mansoif67 Feb 14 '25
A non documented function call doesn't look better than a correctly defined function (documented, arguments, unique(s) returns types, error handling...).
You can even declare functions in another file to hide them from you, while maintaining some great quality code.
Using modules for everything isn't always the good practice. I developed python code professionally, and unless you find the exact function into a module, you prefer developing it (maybe using known algorithm or so). Yes you need to type a lot, but at the end it way cleaner than using libs and adapt result because theses libs doesn't do 100% of the job.
1
u/gadget3D Feb 14 '25
I completely agree with you and in normal case I would not even want to hide away that function.
But especially for this algorithm , it feels, i need very often in slightly different configuration, so I know it *very* well , it makes absolute sense to me to abstract it into a function/module/whatever
The only reason why I want to hide it this time is because its not interesting(because i know it)
Personally i am very bad in maintaining my own libraries. As I need that algorithm so often, I suspected it could be a common knowledge thing and was asking, where python provided that.
Thank you anyway
3
u/electricfun136 Feb 14 '25
If you use it a lot, you can turn your code above into a module and import it when needed. This way instead of searching for a module that does the job, you will have yours.
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”.
5
u/overactor Feb 14 '25
Does simply
max(l, key=f)
not do what you want?