Grasshopper

algorithmic modeling for Rhino

Hello,

I am struggeling with the following (maybe it is quite simple):

I am looking for a python script to zip two lists (#1: numbers and #2: point list) and then sort them by y-value and then by x-value. For example:

#1: Numbers                #2: Point list

1: -39.3                        {0.35, 0.3, 0.0}

2: -439.9                      {1.25, 0.3, 0.0}

3: 513.2                       {1.65, 1.3, 0.0}

4: -10.2                        {1.25, 1.3, 0.0}

5: 326.1                        {0.35, 1.3, 0.0}

Now the desired list is list #1 sorted based upon list #2 (first by y-value then by x-value):

#1: Numbers                #2: Point list

1: -39.3                       {0.35, 0.3, 0.0}        

2: -439.9                     {1.25, 0.3, 0.0}

3: 326,1                       {0.35, 1.3, 0.0}

4: -10.2                       {1.25, 1.3, 0.0}

5: 513.2                       {1.65, 1.3, 0.0}

Thanks in advance

Views: 619

Attachments:

Replies to This Discussion

Hello,

A couple of list comprehensions should do the trick:

## Script start ##

# get the y-values from your points list
y_values = [pt[1] for pt in points]

# zip the points and y_values lists together and sort by the y_values
sorted_pts_y = [pt for (y, pt) in sorted(zip(y_values, points))]

# get the x-values from your now freshly sorted points list
x_values = [pt[0] for pt in sorted_pts_y]

# zip the sorted_points_y and x_values lists together and sort by the x-values
sorted_pts_xy = [pt for (x, pt) in sorted(zip(x_values, sorted_pts_y))]

## Script end ##

If your not familiar with Python list comprehensions, simply google them to find out more about it.

Basically, this:

    y_values = [pt[1] for pt in points]

Is the same as this:

    y_values = []
    for pt in points:
        y_values.append(pt[1])

Furthermore, this part:

    sorted_pts_y = [pt for (y, pt) in sorted(zip(y_values, points))]

Is the same as this:

    sorted_pts_y = []
    for (y, pt) in sorted(zip(y_values, points)):
        sorted_pts_y.append(pt)

Here is an example.

Attachments:

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service