5

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))  
3
  • As a side note, control statements in Python do not need (and should not have) parentheses: while len(queue) > 0:. But, since 0 is false and any other integer is not, this is identical to while len(queue):. And, since an empty sequence is false and any other sequence is not, this is identical to while queue:. And that's how you should write it—it's more idiomatic, easier to read, shorter, and possibly even more efficient. Commented Jan 31, 2013 at 23:49
  • One more problem I just noticed: You're passing queue as the first argument to queue.sort. Whatever type queue is, that's not right. (Oh, and don't call a variable queue; that's the name of a standard library module.) Commented Feb 1, 2013 at 0:18
  • Also: sort is not going to change the length of the object, so this is just going to loop forever, re-sorting an already-sorted list over and over. Commented Feb 1, 2013 at 1:19

1 Answer 1

5

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 argument key:

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

Sign up to request clarification or add additional context in comments.

5 Comments

+1. But it's probably worth explaining that, as orginally written (in 2.x), the OP is using the lambda as a cmp function, and that's exactly why it's not working. (In Python 3, the code would actually work as written.)
Ah, thank you. I didn't know the order of the keyword arguments and didn't want to look it up. I think the key take-away here is that it is far safer to explicitly specify keyword arguments.
Definitely agreed. Besides being safer, and making your code work with both 2.x and 3.x (and IIRC, 2to3 won't fix this problem), it's also much easier to read. Nobody remembers what the order is. The only reason to ever use a cmp function without a keyword is if you need to be compatible with Python 2.3 or earlier.
is the x in the lambda function symbolizing each element in the list?
Yes; the x represents each element, and the function itself returns the value associated with that element for the purpose of sorting the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.