Hi
Let's suppose that I have a list of elements, and I want to select only some of them, according to a certain function (for example a distance to an other element).
I want to have as a result a list of tuple, with the distance and the element. So, I wrote the following code
result = [ ( myFunction(C), C) for C in originalList if myFunction(C) < limit ]
But myFunction
is a very time-consuming function, and the originalList
quite big. So doing like that, myFunction
will be call twice for every selected element.
So, is there a way to avoid this ??
I have two other possibilities, but they are not so good:
the first one, is to create the unfiltered list
unfiltered = [ (myFunction(C),C) for C in originalList ]
and then sort it
result = [ (dist,C) for dist,C in unfiltered if dist < limit ]
but in that case, I duplicate my
originalList
and waste some memory (the list could be quite big - more than 10,000 elements)the second one is tricky and not very pythonic, but efficient (the best we can do, since the function should be evaluated once per element).
myFunction
stores it last
result in a global variable (lastResult
for example), and this value is re-used in the List comprehensionresult = [ (lastResult,C) for C in originalList if myFunction(C) < limit ]
Do you have any better idea to achieve that, in an efficient and pythonic way ??
Thanks for your answers.