Distance between point to list of points

Viewed 3107

I'm trying to calculate the distance from point p to each of the points in list s.

import math
s= [(1,4),(4,2),(6,3)]
p= (3,7)

p0,p1=p
dist=[]

for s0,s1 in s:
    dist=math.sqrt((p0[0] - p1[0])**2 + (s0[1] - s1[1])**2)
    dist= dist+1
    print(dist)

TypeError                                 Traceback (most recent call last)
<ipython-input-7-77e000c3374a> in <module>
      3 dist=[]
      4 for s0,s1 in s:
----> 5    dist=math.sqrt((p0[0] - p1[0])**2 + (s0[1] - s1[1])**2)
      6 
      7 

TypeError: 'int' object is not subscriptable

I see that accessing the location is ceased as p0,p1 are ints. but in this scenario I'm not getting an idea how to address this.

6 Answers

You are accidentally using indexing on your data even though you already separated your points into x, y. In addition, you are overwriting your list and not saving the data. Also the distance formula is incorrect it should be a subtraction between points not addition. Try this:

import math
s= [(1,4),(4,2),(6,3)]
p= (3,7)

p0,p1=p
dist=[]

for s0,s1 in s:
    dist_=math.sqrt((p0 - s0)**2 + (p1 - s1)**2) #Edit this line to [0]s and [1]s
    dist_= dist_+1 #Also change name and/or delete
#    print(dist)
    dist.append(dist_) #Save data to list

dist=math.sqrt((p0[0] - p1[0])**2 + (s0[1] - s1[1])**2)

Here, you are indexing integer.

Moreover, you've made mistake in calculation. It should be:

dist=math.sqrt((p0 - s0)**2 + (p1 - s1)**2)

If what is desired is a list of the distances, that can be done in a single line of code with a list comprehension:

import math
import pprint

s = [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)]
p = (3,-4)

dists = [math.sqrt((p[0]-s0)**2 + (p[1]-s1)**2) for s0, s1 in s]

pprint.pprint(dists)

The other thing here is that I've removed the dist = dist + 1 from the OPs code. I don't see that being correct...why add 1 to each computed distance?

Result:

[6.324555320336759,
 8.0,
 6.4031242374328485,
 4.242640687119285,
 10.44030650891055,
 8.94427190999916,
 5.0,
 5.0,
 3.605551275463989]

Maybe try to change this line:

    dist=math.sqrt((p0[0] - p1[0])**2 + (s0[1] - s1[1])**2)

To:

    dist=math.sqrt((p0 - p1)**2 + (s0 - s1)**2)

If you would like the Euclidean distance, you could do something like this (even without import math)

s = [(1, 4), (4, 2), (6, 3)]
p = (3, 7)

for point in s:
    sum_ = sum((p[i] - point[i]) ** 2 for i in range(len(p)))
    distance = sum_ ** (1 / 2)  # take the square root, the same thing as math.sqrt()
    print(p, point, round(distance, 1))

Results:

(3, 7) (1, 4) 3.6
(3, 7) (4, 2) 5.1
(3, 7) (6, 3) 5.0

The error you get in your code is because you used indexing on an integer. Just like doing this:

>>> a = 3
>>> a[0]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    a[0]
TypeError: 'int' object is not subscriptable

In case you are not constrained by the packages you can use. An implementation using NumPy would be more swift.

import numpy as np

s = np.array([(1,4),(4,2),(6,3)])
p = np.array((3,7))

dist = np.linalg.norm(p - s, axis=1)

Result:

array([3.60555128, 5.09901951, 5.])
Related