I have the following array in Python:
points_list = [point0, point1, point2]
where each of points_list is of the type:
class point:
__init__(self, coord, value):
self.coord = numpy.array(coord)
self.value = value
# etc...
And a function:
def distance(x,y):
return numpy.linalg.norm(x.coord - y.coord)
And I have a point point_a defined elsewhere. Now I want to find the point in points_list that’s closest to point_a.
Other than a loop, what’s the best way to do this in Python?
Have you tried this?
To answer a question in comment:
lambdais indeed necessary here since function specified as akeyargument needs to accept only a single argument.However, since your
point_ais essentially global you could “hard-code” it into thedistancefunction:This way you could pass
distanceas a key argument skippinglambdaaltogether.