# Group splines by height using animation nodes

I have a list of splines coming from an object, I want to group them by their height.

An example would be:

a list of splines:

0 spline 1 with height  0.25
1 spline 2 with height  0.35
2 spline 3 with height  0.45
3 spline 4 with height  0.25
4 spline 5 with height  0.35
5 spline 6 with height  0.65

The result I need is a list of lists:

0 [spline 1 with height  0.25, spline 4 with height  0.25]
1 [spline 2 with height  0.35, spline 5 with height  0.35]
2 [spline 3 with height  0.45]
3 [spline 6 with height  0.65]


Is there a way to do so using nodes or an expression is needed?

Finally a script did the job.

splines = ["S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10"]
heights = [3, 7, 3, 15, 15, 7,]
uniques_heights = list({ h for h in heights})
print(sorted(uniques_heights))
splines_per_heights = []
for h in sorted(uniques_heights):
spline_h = []
for i in range(len(heights)):
if heights[i] == h:
spline_h.append(splines[i])
splines_per_heights.append(spline_h)
print(splines_per_heights)


The script basically takes two lists, one filled with splines and one with the heights for each spline. First creates a dictionary representing each unique height. Then identifies all splines sharing same height and appends them into a list resulting in a list of splines for each height, and lastly it creates a final list including all the lists per height.

Here the result once inside the nodetree.

• Do you mind adding an explanation? Dec 18 '20 at 8:40