Andrey Lyubimov
Andrey Lyubimov

Reputation: 673

Respected argument for max() function in Python

I have this code:

def find_smallest_circle_sqtime(list):
    inv_number_of_elements = 1.0 / len(list);
    smallest_circle = MyCircle(inv_number_of_elements * sum(p.get_x() for p in list),
                               inv_number_of_elements * sum(p.get_y() for p in list), .1);

    smallest_circle.radius = max(smallest_circle.centre.sub(p).norm() for p in list);
    return smallest_circle

Where I'm getting the radius of the farthest point in list relative to the smallest_circle.centre. But actually I need to get the point itself. How can I do that in the most pythonic way?

Upvotes: 0

Views: 65

Answers (1)

vaultah
vaultah

Reputation: 46533

The max function allows you to provide a custom ordering function (via the key argument):

max(list, key=lambda p: smallest_circle.centre.sub(p).norm())

And please don't use semicolons. And you shouldn't use names of built-in types/functions as variable names.

Upvotes: 5

Related Questions