-1

I am having trouble with the lambda function and not really understanding it completely. I am trying to calculate the manhattan distance between two points, 1 point is the current location and the second point is from a list that contains multiple positions. Any help is appreciated, thank you

my function is:

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

I am trying to get a value out of the toVisit list but don't know how to exactly. I was thinking of passing in x with the curr location but that does not work.

8
  • Where are you getting curr from? Commented Feb 1, 2013 at 1:17
  • Unless toVisit is a very unusual type, you should not be passing it as the first argument to toVisit.sort. It doesn't need a copy of itself. And I already explained that in a comment to your last question. Commented Feb 1, 2013 at 1:18
  • curr is given from the parameter, as the current position of pacman, (x,y) and the list contain the 4 corner position in the form (x,y) Commented Feb 1, 2013 at 1:18
  • @abarnert , yes I saw it but did not understand what you meant exactly Commented Feb 1, 2013 at 1:19
  • We would expect to see code like this: something.sort() We do not expect to see code like you have: something.sort(something) In Python, method functions on an object do not need a reference to the object passed in to the call. The only place I can think of where I would expect to see something like that is plain C code. Commented Feb 1, 2013 at 1:25

2 Answers 2

1

Look at the documentation (or built-in help, or how-to. The signature for list.sort is:

s.sort([cmp[, key[, reverse]]]) sort the items of s in place    (7)(8)(9)(10)

The notes explain, among other things, what each of those parameters mean. In particular, the first parameter, cmp:

specifies a custom comparison function of two arguments (list items)

You're passing toVisit as the first argument. So, toVisit will be used as a comparison function. You can't possibly have thought that was what you wanted. So, I'm guessing you probably don't get the basics of objects and dot notation, and you need to read an appropriate tutorial. (Sorry, I don't have one to recommend. I checked the official Python tutorial, but it seems to assume that dot notation, and what a method is, and so on are all just obvious and need no explanation…)

Here's the right version:

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

Meanwhile, there doesn't seem to be any problem with the lambda function itself, but you seem to be convinced, in two questions in a row, that the lambda is the part you're having trouble with. If you ever have trouble with a lambda, the easiest thing to do is transform it into a normal named function, which is trivial to do.

Where you have this:

lambda ARGS: EXPR

Do this a line above it:

def good_name_for_what_expr_does(ARGS):
    return EXPR

Then, replace the lambda ARGS: EXPR with good_name_for_what_expr_does.

So, this is equivalent to your original code:

def manhattanDistanceFromCurr(x):
    return util.manhattanDistance(curr, x)
toVisit.sort(toVisit, key = manhattanDistanceFromCurr)

Can you see the problem there any more easily than in the lambda version?

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

Comments

0

I suggest you do a Google search for "Python introduction to lambda". Here's the first result, which is a nice explanation:

http://pythonconquerstheuniverse.wordpress.com/2011/08/29/lambda_tutorial/

Comments

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.