Grasshopper

algorithmic modeling for Rhino

# Sort ContourCurves by height Python like with the rhinocommand

hello,

Basically i wanto to group contours by plane like you can with the rhino function. In GH i do this creating a set, now i tried to do this in python. i found this post at Steve Baers blog

https://stevebaer.wordpress.com/2011/10/18/python-goodies-the-sorte...

and tried to use it in GH.

But it wont work. Somebody knows what i am missing?

Thanks and regards!

Views: 1367

### Replies to This Discussion

Hi Chapulin, attached a file that demonstrates two general methods for sorting data values in Python based on other data keys:

1) If the keys to sort by is a property of the data to sort, use a lambda function.
2) If the keys to sort by is NOT a property of the data to sort, zip the two lists and sort this.

Note that there are many more options, these are just two of the patterns I tend to use a lot (mainly because of their terseness).

/A

Edit: So in your case (if I understood you correctly i.e. sort some curves by height i.e. a Z value on the curve), this could be solved using method 1 like so:

Attachments:

Yup, looks like the base logic is the same as method 2 in my example. Note that tuples in Python is a data structure (an immutable array, where lists are mutable).

Agreed, just read your code again and see what you were doing. Think I was just confused by this line on my first read: tuples = []  ;)

Hi Tom hi Anders,

thank you both for yime and your codes! Toms sortByPlaneWithGroups was exactly what i was looking for. Thanks also for the explication. Your conversation is really helpful for me!

In anders version i like that is a really small code that hepls me understanding the basics of list and sorting lists.  Adding datatrees like in toms version is something i need to learn but its still really andvanced for me. Even in GH i am still struggling often with them.

I assume ,tom, that you began with c#? i was thinking of switching at some point to c# cause of its usage in the aec sector. Generally i searched python examples for architecture but i did not found so much and reading the forum i got the impression(maybe just because of the arguments of peter,the lord of the darkness,fortiades) that c# dominates the sector.

Do you think i should switch to c# or can i reach the same with python or is it just  personal preference?

regards

hmm, i thoguht to begin with python cuase you dont have to define classes and everything and it seems to me easier to read than other languages with a lot of parentesis.

Like you said its more important to understand the rhinocommon, there are still many questions i have and everybdy says there are so many resources- and yes there are any but if compare with GH resources i miss the practical application examples, maybe cause the most of ocassions code is written is for something very escpecial.

I try to get codes to understand here in this forum and in the rhino forum but there are not so much, or i just dont find them?

I found some resources like the atlv site http://atlv.org/education/ghpython/

or the designalyze homepage but its all about things like make a cube with different color evey dot or things like this.

The one i most like cuase of its content is this series https://www.youtube.com/watch?v=zcr7Gh3Wkdo about an animation using GhPython.

May i ask you what problems you give to your students or a rough plan about the teaching content? i still feel a little lost in some basic questions like when do you import/use rhino and when rhinoscriptsyntax

Hi Tom,

many thanks for yor tips and eplications! i will think about switch to c# already now and look more into rc and try to use classes.

regards from the federal district of mexico!

A general best practice note (in Python): Do NOT use classes to structure code, use functions, modules and packages for this (that's what they are designed for). Use classes when it's appropriate i.e. when constructing objects (that have data and methods associated with them, for instance for developing a agent-based system or defining a louvre for a facade etc.).

Edit: Also yes, I would recommend to stick to RhinoCommon when writing code for GHPython. At least for most things. Rhinoscriptsyntax has some useful functions for interacting with the Rhino document. Also, note that you can find the source code for rhinoscriptsyntax on your system, roughly here: