Filter list by points

I have approximately 12 points, numbered from 0 to 11. Point 0 serves as my starting point, while point 11 is the end point. Points 1 through 10 lie between them. I want to create pairs such as (0, 1), (2, 3), (4, 5), continuing up to (10, 11). However, the number of points may increase if I add extra points, so I would like the last point to always be paired with its nearest neighbor (for example, 10 and 11). Please suggest how I can achieve this.

How I would go about it:

  1. Starting with your original list of points.
  2. Line.ByBestFitThoughPoints to generate a line that follows all the points. Note that depending on the type of shapes you’re using an Arc.ByBestFitThoughPoints might be a better fit - the rest of the concepts all hold true.
  3. Curve.ParameterAtPoint to get the paramter of each point (from 1) along the curve (from 2).
  4. List.SortByKey to sort the list of points (from 1) by the parameters (from 3).
  5. PolyCurve.ByPoints to generate a series of connected lines along the point sequence.
  6. PolyCurve.Curves to extract the individual lines (if needed)
1 Like

normally, i would just:

def group_adjacent(items, group_len):
    return [items[i:i + group_len] for i in range(0, len(items), group_len)]

OUT = group_adjacent(IN[0], 2)

However, the number of points may increase if I add extra points, so I would like the last point to always be paired with its nearest neighbor (for example, 10 and 11)

i find it a bit confusing. u want the last two items to be always coupled, regardless of the items count? but if there are an odd number of items in the list, there will always be one that is left alone.

Thank you so much for the quick response!
Is there any other way to find the end result that I marked with an arrow?

Here are some solutions using OOTB nodes - note the list levels on the bottom group

2 Likes

I worked out you can do the sliding window method with fewer nodes

1 Like