python - Finding closest pair of coordinates from a list -


i attempting implement solution previous so question

i have pair of coordinates wish find closest associated pair of coordinates in list of coordinates.

this can achieved finding pair minimum distance between points:

dist = lambda s,d: (s[0]-d[0])**2+(s[1]-d[1])**2 

i have dictionary, origin:

{u'toid': u'osgb4000000029928750', u'point': [524511.405, 184846.794]} 

i have list containing pairs of coordinates, d_origins:

[(532163.5648939193, 181848.77608212957),(532449.8292416488, 181847.71793660522), (532200.2156880093, 182053.30247829395), (533794.6284605444, 181119.5631480558)] 

i attempt find match value calling dist lambda function:

match = min((origin[0]['point']),key=partial(dist,d_origins)) print origins, match 

however, output is:

typeerror: 'float' object has no attribute '__getitem__' 

the min function takes list or iterable. additionally ordering can specified function 1 argument. means function maps element value, compared min function find minimum element.

match = min(d_origins, key=lambda p: dist(p, origin['point'])) 

the lambda expression wraps dist function 2 arguments function 1 argument providing 1 of arguments. new anonymous function compares coordinate specific origin in lambda expression. then, result of min function closest coordinate origin.

full example:

>>> dist = lambda s,d: (s[0]-d[0])**2+(s[1]-d[1])**2 >>> origin = {'toid': 'osgb4000000029928750', 'point': [524511.405, 184846.794]} >>> d_origins = [(532163.5648939193, 181848.77608212957),(532449.8292416488, 181847.71793660522), (532200.2156880093, 182053.30247829395), (533794.6284605444, 181119.5631480558)] >>>  >>> match = min(d_origins, key=lambda p: dist(p, origin['point'])) >>> print(str(match)) (532200.2156880093, 182053.30247829395) 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -