The Euclidean Distance is the square root of the squared differences of the elements of two lists (of the same length).
import math; def Euclidean(x,y): #x,y are lists of the same length length = len(x); S = 0; #The sum of the squared differences of the elements for i in range(length): #Substract x[i] from y[i] and square it S += math.pow(x[i]-y[i],2); return math.sqrt(S); #The square root of the sum print Euclidean([1,2],[3,4]);
The Hamming Distance is the number of differences in two lists of the same length, element by element. Usually the elements hold binary values. For example, the Hamming Distance of (1,0,1) and (1,1,1) is 1, as only the middle element is different in the two lists.
import math; def Hamming(x,y): #x,y are lists of the same length length = len(x); Counter = 0; #The number of differences between x and y for i in range(length): if(x[i] != y[i]): #Found a difference between x and y, #increment Counter Counter += 1; return Counter; print Hamming([1,0,1],[1,1,1]);