python - How to order a list of points by distance to a given point? -


i have list of items have x , y coordinates. now, there's method takes x , y parameters , should return list of coordinates ordered closest farthest based on given parameters.

basically, looks this:

class point:     x = 0.0     y = 0.0  # list of points points = ...  def get_ordered_list(x, y):     # return 'points' ordered distance (x,y) 

i'm new python have pretty no idea how order items. how that?

you can pass custom function sort using key parameter this:

from math import sqrt  def get_ordered_list(points, x, y):    points.sort(key = lambda p: sqrt((p.x - x)**2 + (p.y - y)**2))    return points 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -