-1

I need to write this code

sorted(labeled_points,key=lambda (point, _): distance(point,new_point))

where distance is just Euclidean distance between vectors. The error message I receive is that it isn't possible in Python 3 - Sublist parameters are not supported in Python 3.x . I tried to replace it with this code

sorted(labeled_points,key=lambda point: distance(point,new_point))

but it seems this isn't working.
I also looked at examples and found that we can make do without lambda, we need to define function

def func(point,new_point):
    return distance(point,new_point)
...
sorted(labeled_points,key=func(new_point))

But this does not seem to be evaluated. So,, how one could write this in Python 3?

sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • 2
    share full error msg log – sahasrara62 Aug 27 '22 at 14:27
  • Dupe: [How to sort a list/tuple of lists/tuples by the element at a given index?](https://stackoverflow.com/questions/3121979/how-to-sort-a-list-tuple-of-lists-tuples-by-the-element-at-a-given-index) – Marco Bonelli Aug 27 '22 at 14:29
  • 1
    And pleas also provide example data which allow to test your code – droebi Aug 27 '22 at 14:29
  • Marco Bonelli In my example, it should be sorted by distance. And sorted(labeled_points,key=lambda point: distance(point,new_point)) is throwing Exception when distance is applied. I don't know how to replace the code without sublist so that it would be equivalent – Strongscientist Aug 27 '22 at 14:52
  • sahasrara62 Errors for "sorted(labeled_points,key=lambda (point, _): distance(point,new_point))" Sublist parameters are not supported in Python 3.x Error (active) Position-only argument separator not allowed as first parameter For "sorted(labeled_points,key=lambda point: distance(point,new_point))" an exception is thrown – Strongscientist Aug 27 '22 at 15:02
  • The exception is specifically "unsupported operand type(s) for -: 'list' and 'float'" - I use their zip function. The error is in this code "return [v_i-w_i for v_i, w_i in zip(v,w)]" – Strongscientist Aug 27 '22 at 15:10

2 Answers2

0

Instead of

sorted(labeled_points, key=lambda (point, _): distance(point, new_point))

do

sorted(labeled_points, key=lambda labeled_point: distance(labeled_point[0], new_point))

See the end of PEP 3113's Transition Plan for a similar example (that's the PEP that removed this Python 2 syntax in Python 3).

Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65
-1

You have a distance function to get the distance between two arbitrary points. You want to sort based on the distance from a fixed point - one of the parameters will always be the same. You can use functools.partial to create a new function that sets static parameters and use that to generate the sort key.

import math
import functools

def distance(v,w):
    return math.sqrt(sum(((v_i-w_i)*(v_i-w_i) for v_i in v for w_i in w)))

fixed_point = (9,9)
points = [(3,4), (20,6), (1,1), (9,9)]
print(list(sorted(points, key=functools.partial(distance, fixed_point))))
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • Ok, maybe I should have written code for distance, but it is "distance(v,w)=math.sqrt(sum((v_i-w_i)*(v_i-w_i) for v_i in v,w_i in w)" -Euclidean distance So I don't think it compares on its own. The distances between points are compared. My problem is syntax, how to realise aforementioned code in Python 3. – Strongscientist Aug 27 '22 at 15:42
  • @Strongscientist - I think I showed how to do it, even if my `distance` function is totally off base. I'm not sure if you wanted to measure relative distance from a fixed point... or from each other (which probably doesn't make sense for a sort). The point was simply that the key is an object that knows how to do the comparison, not the comparison itself. Each object in the list will only get 1 key object, but each key object will be used several times for the compare. – tdelaney Aug 27 '22 at 15:52
  • It sorts by the distance between points from labeled_points list and a fixed point (called new_point). I am still not sure about how to do a distance function doing a compare itself, because a distance between new point and itself is zero, but I will try to modify the example. I would have liked your post, but can not do it yet( – Strongscientist Aug 27 '22 at 16:11
  • @Strongscientist - got it. There is a different and easier solution for that. I'll update. – tdelaney Aug 27 '22 at 16:22
  • I mean new_point is given. By your notation it isn't used.Shouldn't it be DistanceCompare(new_point)? – Strongscientist Aug 27 '22 at 16:32