List or Tuples

I am getting the point name and its co ordinates of a 3D point in my workspace then want to sort by either of the fields. What would you recommend ? a list or tuples if so is there an easy way of making either set of data to sort it. I have looked at lists and lambda values would this be the easiest way. Dont think my list is working in the example below.

import rhinoscriptsyntax as rs
MeasPts = [ ]
objectIds = rs.GetObjects("Pick measured points", rs.filter.point)
#for id in objectIds: print "Object identifier:", id
for id in objectIds:
    objectname = rs.ObjectName(id)
    if objectname is None: rs.ObjectName(id,"No name")
    objectname = rs.ObjectName(id)
    objectcoord = rs.PointCoordinates(id)
    print objectname,objectcoord[0],objectcoord[1],objectcoord[2]

If I am understanding you correctly, this is what you are looking for. You need to append the points to the your array list.

    import rhinoscriptsyntax as rs

measPts = [ ]
objectIds = rs.GetObjects("Pick measured points", rs.filter.point)

for id in objectIds:
    objectName = rs.ObjectName(id)
    if objectName is None:
        rs.ObjectName(id,"No name")
    measPts.append(id)
    objectcoord = rs.PointCoordinates(id)

    print "Sort by Coord: " + str(id)
    print "Sort by Name: " + objectName

What is MeasPts supposed to be in the above sample?

Hi Steve,

They are 3d points in my workspace some with names and some without.
I want to select them and the be able to sort the data set by either name,X,y or z value.

Roger

If you are going to sort or modify the items in-place, use a list, not a tuple. Tuples are immutable (you can’t modify them). You can of course always make a sorted copy.

Hi,

I am still getting slightly confused I can get the following output from my script

import rhinoscriptsyntax as rs

objectIds = rs.GetObjects(“Pick measured points”, rs.filter.point)

#for id in objectIds: print “Object identifier:”, id

for id in objectIds:

objectname = rs.ObjectName(id)

if objectname == “None”:objectname== “No Name”

objectcoord = rs.PointCoordinates(id)

print objectname,",",objectcoord

3 , -170.746075173116,200.24702273105,-23.3611932323999

2 , -181.501607487389,15.6103846693673,-23.3611932323999

1 , -260.37551112539,-149.307777482816,-23.3611932323999

No name , -56.0203971542065,-215.633560087498,-23.3611932323999

No name , 196.734612231204,-228.181681120816,-23.3611932323999

No name , 318.630645126296,-72.2264625638606,-23.3611932323999

6 , 306.082524092978,123.165707812095,-23.3611932323999

5 , 189.564257355022,241.476563269096,-23.3611932323999

4 , 114.275531155113,259.402450459551,-23.3611932323999

No name , 35.4016275171123,80.143578555004,-23.3611932323999

No name , 38.9868049552032,-36.3746881829513,-23.3611932323999

No name , 169.845781445522,-75.8116400019515,-23.3611932323999

No name , -45.2648648399337,-106.285648225724,-23.3611932323999

Which are the name, x y and z of the co ordinated points in my workspace. I want to be able to make a list that I can then sort all the data on either the name or x or y or z values ascending or descending. Un fortunately the more I read about lists the more I get confused as I want the data to be shown as above as rows not one long list that is hard to read.

So I need help with creating a list, sorting a list and displaying a list in the sorted manner the Rhino function side of things is ok, I think.

Kind Regards

Roger Davies

Geo-Spatial Survey Solutions Ltd

Mobile 07713 160041

WWW.geo-spatial.co.uk

HI, == and = are different in most programming languages.
The second == should be =. I don’t know why it’s working…

And as for sort, as you say, I think a list of tuple and lambda is the most standard way to do what you want.

ll=[]
for id in objIds:
    #get objcoord
    ll.append((id,objcoord))  #list of tuples
newll=sorted(ll,key=lambda (id,coord):id)  #if you want to sort by id
newll=sorted(ll,key=lambda (id,coord):coord.X)  #if you want to sort by X
for id,coord in newll:
    print id,coord

Hi,

Now I get my list measPts how do I sort that on name, x,y, or z ? Can I show the list in a better way not one long string ?

It’s quite hard shifting from VB script to this as the mind set needs to change, but I will persevere.

Kind Regards

Roger Davies

Geo-Spatial Survey Solutions Ltd

Mobile 07713 160041

WWW.geo-spatial.co.uk

Many thanks for the assistance I will give your suggestion a try and well spotted for the == I have changed that.

You are welcome!
It is not list vs tuple. list of tuples instead.

sorted is the python function you’ll want to research. Here’s a short post I wrote a long time agoon this subject

1 Like

Just to add a few more examples of sorting points by some property using sorted():

These are implemented in GHPython, but should work the same in the EditPythonScript editor.

Hello,

You can also use the list.sort() method to sort an existing list without creating a new one

from pprint import pprint
import rhinoscriptsyntax as rs

points = rs.GetObjects()

# sort by object name
points.sort(key=rs.ObjectName)

pprint(map(rs.ObjectName, points))

# sort by X then Y then Z
points.sort(key=rs.PointCoordinates)
pprint(map(rs.PointCoordinates,points))

# sort by Z
points.sort(key=lambda x: rs.PointCoordinates(x)[2])
pprint(map(rs.PointCoordinates,points))
1 Like

Thanks very much, that way seems quite straight forward, so much to learn so little time.

Many thanks much appreciated.