Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active January 16, 2017 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/82e988635306a6f144ecb711fa5760ec to your computer and use it in GitHub Desktop.
Save thmain/82e988635306a6f144ecb711fa5760ec to your computer and use it in GitHub Desktop.
def UpdateNeighbors(neighbors,item,distance,k):
if(len(neighbors) < k):
#List is not full, add new item and sort
neighbors.append([distance,item["Class"]]);
neighbors = sorted(neighbors);
else:
#List is full
#Check if new item should be added
if(neighbors[-1][0] > distance):
#If yes, replace the last element with new item
neighbors[-1] = [distance,item["Class"]];
neighbors = sorted(neighbors);
return neighbors;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment