Sorting and Grouping List items with a reference nested list data structure

I am trying to create a standardized method to sort and regroup a list of Dynamo Curves based on a nested list structure of Dynamo Points (which were previously obtained from the same Curves). A marked-up screenshot of the Dynamo Script is attached below for better understanding.

My Current Script is below:

# The inputs to this node will be stored as a list in the IN variables.
Crvs = IN[0]
MidPts = IN[1]

# Place your code below this line
########## Finding the MidPoints on Curve ##########
Pts = []
for i in Crvs:
	x = i.PointAtParameter(0.5)
	Pts.append(x)

########## Sorting the Crvs based on reference MidPts ##########
List = []
for i in MidPts:
	templst = []
	for j in i:
		for a in Pts:
			if j == a:
				y = Pts.index(a)
			templst.append(Crvs[y])
	List.append(templst)
	
# Assign your output to the OUT variable.
OUT = List

I am looking to either fix the current script or to figure out a better/efficient way to work.

Your help is very much appreciated!
Thanks!!

The error gives you a pretty big clue. You’re only defining y when j == a, but you’re appending Crvs[y] regardless. You need to move the append function under your if indent.

That being said there is probably a much more direct way to get what you want. A better explanation of what you’re doing and what you’re trying to accomplish would help.

2 Likes

@Nick_Boyts
What I am trying here might be a crude approach. As a Python beginner I would love to learn any efficient approach!

But my intent here is to reorganize the ‘Curves input list’ by taking the list structure reference from the ‘MidPoints input list’. The relation between the two inputs here is that each point in the ‘MidPoints input list’ has a Curve from the ‘Curves input list’ that has the same midpoint.

For better representation, please check the Inputs, known assumptions and desired Outputs in the code block below.

Crvs = [C1, C2, C3]
MidPts = [[P1,P3],[P2]]

# Below are the known assumptions
# cP1, cP2, cP3 are the midpoints of C1, C2, C3 respectively.
# cP1 = P1 , cP2 = P2 & cP3 = P3

# Intend Result/Output:
OUT = [[C1,C3],[C2]]

How are you getting these midpoints? It seems like you’re getting them and then reordering your curves when you could probably just get them in the correct order.

The best way is probably with a dictionary.

Yes, I just figured the same. I was able to get the results right using the dictionaries method in Python Online Compiler (Screenshot below) but I am struggling to get the same implemented in the Dynamo IDE. :frowning:

I think I figured it without the dictionaries, not sure if that’s the right way. :confused:

I’m still wondering about how you’re getting your list of MidPts to begin with. I want to see the rest of your graph. You have a list of curves and you have a list of their midpoints. Why are they already not in the same order?

Hello
to compare geometries (points) you can use:

  • geometry.DistanceTo(geometry)
    or
  • geometry.IsAlmostEqualTo(geometry)

2 Likes