Skip to content

Instantly share code, notes, and snippets.

@martinogden
Created October 29, 2012 13:28
Show Gist options
  • Save martinogden/3973545 to your computer and use it in GitHub Desktop.
Save martinogden/3973545 to your computer and use it in GitHub Desktop.
n-dimensional Euclidean Distance
def euclidean_distance(p, q):
""" Calculate distance between 2 points in n-dimensional space
>>> euclidean_distance((3, 0), (0, 4))
5.0
"""
n = len(p) # dimensions
return sum([(p[i] - q[i]) ** 2 for i in xrange(n)]) ** 0.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment