def _normalized_levenshtein_distance(s1, s2, acceptable_differences):
 if (len(s1) > len(s2)):
  (s1, s2) = (s2, s1)
  acceptable_differences = set(((- i) for i in acceptable_differences))
 distances = range((len(s1) + 1))
 for (index2, num2) in enumerate(s2):
  new_distances = [(index2 + 1)]
  for (index1, num1) in enumerate(s1):
   if ((num2 - num1) in acceptable_differences):
    new_distances.append(distances[index1])
   else:
    new_distances.append((1 + min((distances[index1], distances[(index1 + 1)], new_distances[(-1)]))))
  distances = new_distances
 return distances[(-1)]

def _normalized_levenshtein_distance ( s1 , s2 , acceptable_differences ) : if ( len ( s1 ) > len ( s2 ) ) : ( s1 , s2 ) = ( s2 , s1 ) acceptable_differences = set ( ( ( - i ) for i in acceptable_differences ) ) distances = range ( ( len ( s1 ) + 1 ) ) for ( index2 , num2 ) in enumerate ( s2 ) : new_distances = [ ( index2 + 1 ) ] for ( index1 , num1 ) in enumerate ( s1 ) : if ( ( num2 - num1 ) in acceptable_differences ) : new_distances . append ( distances [ index1 ] ) else : new_distances . append ( ( 1 + min ( ( distances [ index1 ] , distances [ ( index1 + 1 ) ] , new_distances [ ( - 1 ) ] ) ) ) ) distances = new_distances return distances [ ( - 1 ) ]
this function calculates the levenshtein distance but allows for elements in the lists to be different by any number in the set acceptable_differences .