Suppose chest is a list of coordinates that are two item lists.
For example chest = [[2,4], [4,5], [1,3]]
With the function below, I want the distance between the point (x,y) and each of the chest points. So as it is, the function would return these three distances, right? But my question is, how do I return only the smallest (or largest) result from these three values? Is there any way to do this without creating a new list of distances?
def makeMove(board, chest, x, y):
for cx, cy in chests:
distance = sqrt(abs((x-cx)**2) + abs((y-cy)**2)))
return distance
You could use
max()ormin()plus a generator expression…Also note that as an optimization, you may prefer to do this instead (since if
sqrt(x) > sqrt(y), thenx > y) to reduce the number ofsqrtcalls:(Also, are you sure you don’t want to be squaring the distances instead of
abs()ing them? The normal distance formula issqrt((x-x')^2 + (y-y')^2)…)