Using lambda in Python

I am trying to use lambda do to sort by list. What I wanted to do was sort the coordinates based on their Manhattan distance from the original position. I know that I have most of the syntax, but it seems like I'm missing something small, thanks!

while (len(queue) > 0):  
    queue.sort(queue, lambda x: util.manhattanDistance(curr,x))  
+5
source share
1 answer

It looks like you are trying to tell a method to sort()use your lambda function as a keyfor sorting. This is done with the keyword argument key:

queue.sort(queue, key = [your lambda function])

Rewritten line:

queue.sort(queue, key = lambda x: util.manhattanDistance(curr,x))

: -; , , ,

+5

All Articles