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 6834945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:10:25+00:00 2026-05-26T23:10:25+00:00

I am trying to compute in Python the length of the path from a

  • 0

I am trying to compute in Python the length of the path from a point A to a point B going through a list of intermediary points. I know how to do it but I do want to use the reduce Built-in function.

Why I tried so far, please note that it is completely wrong, is this:

reduce(lambda x,y: math.sqrt((y[1]-y[0])**2+(x[1]-x[0])**2) , ((1,2),(3,4),(1,8)))

Any idea?

Thanks.

  • 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-05-26T23:10:26+00:00Added an answer on May 26, 2026 at 11:10 pm

    You should map before you reduce.

    points = [(1, 2), (3, 4), (1, 8)]
    distances = (math.hypot(b[0]-a[0], b[1]-a[1])
                 for a, b in zip(points, points[1:]))
    total_distance = sum(distances)
    

    or, if you must use reduce(), although sum() is better for this purpose:

    import operator
    
    total_distance = reduce(operator.add, distances)
    

    If you have a lot of points, you might find NumPy helpful in doing this all at once, quickly:

    import numpy
    
    total_distance = numpy.hypot(*numpy.diff(numpy.array(points), axis=0)).sum()
    

    Edit: use math.hypot() and add NumPy method.

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

Sidebar

Related Questions

I'm trying to compute clusters on a set of points in Python, using GeoDjango.
I'm trying a solve an exercise from Exploring Python book. But, I guess I
Im trying to compute a 24 hour rms (root mean squared) from a dataset
I'm trying to make my python script run upon startup but I get the
I'm trying to use gstreamer 0.10 from Python to simultaneously display a v4l2 video
I know there have been plenty of questions about this, but I've been trying
(Step 1) I'm trying to get openCV to run from python on my mac
I am trying to use some AOP in my Python programming, but I do
I'm trying to get a list of currently-open sessions in Python via WMI. What
For a class project I'm trying to do some socket programming Python but running

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.