I am trying to use lambda do to some sorting on a list. What I wanted to do is sort the coordinates based on their manhattan distance from an inital poisition. I know I have most of the syntax down but it seems like I am missing something small, Thanks!
while (len(queue) > 0):
queue.sort(queue, lambda x: util.manhattanDistance(curr,x))
It appears that you’re trying to tell the
sort()method to use your lambda function as the key for sorting. This is done with the keyword argumentkey:queue.sort(queue, key = [your lambda function])The rewritten line is:
queue.sort(queue, key = lambda x: util.manhattanDistance(curr,x))EDIT: misunderstood the purpose of the original lambda function; thought it was intended as a comparison function, which doesn’t make sense since distance functions can’t be negative