Sort List with lambda fuction

(Sonia Ruta) #1

Hi! I try to sort the list “point0” defining a lambda fuction, so like:


But it’s not working. Anyone know how I can do?
Thank’s.

(Graham) #2

Hello,

You should give a callable as the key to your sorted method so if I understand your script correctly the parameter should be key=lambda pt: pt.DistanceTo(itemPt0) note the capitalised To

Note that you can also sort the list in place by calling point0.sort(key=... to avoid creating a duplicate list, if that is useful?

-g

(Graham) #3

I don’t understand lines 37 (incomplete) and 38 (I dont think there is anything to unpack - your list contains Point3d objects, no?)

(Sonia Ruta) #4

Sorry, I rewrite the script

(Graham) #5

Did you see my other message above :slight_smile: :grey_question:

(Sonia Ruta) #6

oh yes! I try with “point0.sort(key=lambda pt: pt.Distanceto(itemPt0))” but it’s not work equally.
Sorry I’m beginner with Python.
What do you advise me to do?

(Graham) #7

Wtih capitalised To in DistanceTo ? Do you get an error message? Can you copy the precise error message here?
Can you create a minimal file with geometry internalised to share to demonstrate the problem?

(Sonia Ruta) #8

This is the error:

sort list.gh (30.8 KB)

(Graham) #9

OK please reread the error message and my replies above. You have the answer in front of you :wink:

(Graham) #10

Here

You have used lower case

(Sonia Ruta) #11

A OK! but I have the same problem with “DistanceTo”

(Graham) #12

OK - sorry my turn not to read properly - you have ‘guid’ objects not point3D. try key = lambda pt: rs.Distance(pt, itemPt0) ???

rhinoscriptsyntax generally returns guid references to objects, not the objects themselves. This makes it a bit tricky combining rs and Rhino functions - you do have a bunch of ‘coerce’ functions which wiil help you work your way across the boundary where needed, e.g. coerceguid, coerce3Dgeometry, …

(Sonia Ruta) #13

Yess!!! it works!!
Thank you so much!