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.
You should map before you reduce.
or, if you must use
reduce(), althoughsum()is better for this purpose:If you have a lot of points, you might find NumPy helpful in doing this all at once, quickly:
Edit: use
math.hypot()and add NumPy method.