Sorting nested lists of points by distance

Hi there,

I have an input of points into my python script in Dynamo, as seen. I want my python script to then sort these cluster of points according to their distance, so that it makes nested loops where each nested loop is the cluster of points nearest to it… This is where i am at so far:
MINE%20ORIGINAL
does this make sense?

could someone help?

many thanks.

So i’d want the output to look like this:
image

Can you show all of your inputs for Python node and the full Python script?

sure:


and the full python script is as follows:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import math

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
cluster_points = IN[0]
tolerance = IN[1]
p = cluster_points[IN[2]]
outputPoints =[]
sorted_list=[]

def sortClusters(points):
    for j in i:
             for i in points:
                   if (p.DistanceTo(i) < tolerance):
                      outputPoints.append(i)
                      cluster_points.remove(i)
                   else:
                       pass
              
output = outputPoints

There are a couple things I am confused by. Why did you flatten all of the points before putting them in the Python script? That got rid of the groups of 4 and turned it all into a single list. Then you want to group back into groups of 4?

Then for the line for j in i:, where did i come from? i wasn’t created until the line after it, so it is impossible to use it before it was created.

Perhaps I have mistaken the task, but shouldn’t this be achievable through the “Sort by keys”? Then use the distance between point as key. May require some usage of levels, etc.

Have created a small sample, that hopefully illustrates my thought process :slight_smile:
Capture

2 Likes

you’re right, thanks - dictionary is a good shout, think I was unsure how to make the nested loop work w it. I don’t end up w a nested list to start w because i flatten it (it’s flattened so that the code can work with whatever the input)… so i think now im just struggling to tie all these things together in python script, any ideas how i can debug/amend?

yes indeed i flattened so that it can be adaptable as a code and work for any input list (i.e. nested or not) and yeah want to sort them according to lengths into clusters…

and the for j in i line was a mistake, was trying to figure out what goes in between.

im here at the moment