Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9224363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:18:03+00:00 2026-06-18T04:18:03+00:00

I am having trouble with the lambda function and not really understanding it completely.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:18:04+00:00Added an answer on June 18, 2026 at 4:18 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble passing a C++0x lambda function as the second argument to makecontext
Having trouble with each function... Will try to explain by example... In my code,
Having trouble understanding how to filter an images table by tag information in a
Having trouble understanding. With the following css : .bloc .field:nth-last-child(2){ ...some values... } and
I am trying to learn about combinators and I am having trouble understand the
I'm trying to use a ThreadPool implementation from this fellow: https://github.com/progschj/ThreadPool I'm having trouble
I'm having trouble with a query written in LINQ and Lambda. So far, I'm
I'm having trouble figuring out why Breeze.js (0.84.3) does not return error messages to
I'm very new to lambda expressions in C#, and I'm having trouble conceptualizing how
I'm having trouble working with lambda functions in the Intel compiler, in particular, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.