I have a high cpu consuming function, lets say:
def high_cost_function(p):
... lots of operations...
return p
which I want to use under an inner loop in python:
Example:
paths1= ['a','b','c']
paths2= ['b','c','d']
[any(x.startswith(high_cost_function(y)) for x in paths1) for y in paths2]
Which returns properly: [True, True, False]
However, as you can see, I am forced to call high_cost_function(y) for each of the x in paths1.
If it was a normal loop, I would probably simply do:
for y in paths2:
tmp_var = high_cost_function(y)
for x in paths1:
...
use tmp_var
...
Is there anyway to implement this in the first "pythonical stylish way"??