Split list by multiple ranges of numbers

Hi there,

I cant seem to achieve such a simple idea. I have a list of values, and I want to split that list into x amount of individual lists which range between sliders.

For example
Slider 1: -501.3 (minimum, or no defined minimum)
Slider 2: 265.3
Slider 3: 403.4
Slider 4: 708.7
Slider 5: 1000.3 (maximum, or no defined maximum)
(Ability to add or remove sliders)

So I have an Input list of values, and in this case, I want all values in separate lists defined by these ranges. e.g.,

Output 1 contains all values ranging between -501.3 and 265.3 (or below 265.3)
Output 2 contains all values ranging between 265.3 and 403.4
Output 3 contains all values ranging between 403.4 and 708.7
Output 4 contains all values ranging between 708.7 and 1000.3 (or above 1000.3)

How can this be achieved?

Best,
Jeremy

Hey Jeremy, is that what you were looking for? It’s probably not the most efficient and I have not handled the case where two domains intersect, e.g. (y to 20/ 20 to x) but maybe gives you a start for a more refined logic. (Also does not handle numbers out of bound)

Domain_Intersection.gh (7.7 KB)

1 Like

A bit more robust implementation, including values that are outside the defined intervals.

private void RunScript(
		List<double> values,
		List<double> bounds,
		bool includeOutliers,
		ref object a)
    {
    bounds.Sort();
    values.Sort();

    var intervals = CreateConsecutiveIntervals(bounds);
    var tree = new DataTree<double>();

    for (int i = 0; i < intervals.Count; i++)
        tree.EnsurePath(new GH_Path(i));

    var below = new List<double>();
    var above = new List<double>();

    
    foreach (double val in values)
    {
        bool assigned = false;

        for (int i = 0; i < intervals.Count; i++)
        {
            if (val >= intervals[i].T0 && val < intervals[i].T1)
            {
                tree.Add(val, new GH_Path(i));
                assigned = true;
                break;
            }
        }

        if (!assigned)
        {
            if (val < intervals.First().T0) below.Add(val);
            else if (val > intervals.Last().T1) above.Add(val);
        }
    }

    if (includeOutliers)
    {
        if (below.Any()) tree.AddRange(below, new GH_Path(-1));
        if (above.Any()) tree.AddRange(above, new GH_Path(intervals.Count));
    }

        a = tree;
    }

    private static List<Interval> CreateConsecutiveIntervals(List<double> bounds)
    {
        var intervals = new List<Interval>();

        for (int i = 0; i < bounds.Count - 1; i++)
        {
            intervals.Add(new Interval(bounds[i], bounds[i + 1]));
        }

        return intervals;
    }
4 Likes

HI Felix, thanks so much for your help!

I don’t know how to setup custom scripts in GH, hoping to learn soon. Can you give me some recommendations on how to get your script working in GH?

Best,
Jeremy

and then just replace the runscript method

1 Like

listing.gh (8.2 KB)

Oops, here’s the list of numbers at the end:

list range.gh (8.0 KB)

Hi there, can the final output be separated into individual containers?

Jeremy

Thanks so much felix

Just wondering if the final output can easily be separated into individual streams such as 1, 2, 3, 4 (number of slider inputs). Then I can separate that data and perform actions on it separately

Best,
Jeremy

The sloppy and easiest way is to use Explode Tree i guess. Or you could use tree branch if you just need to select specific branches depends a bit on your needs

1 Like

You might prefer all the data coming out of a LIST ITEM (red group) but it’s maybe a bit unnecessary. Kinda wish there was a BRANCH ITEM that had multiple outputs the way LIST ITEM does.

BRANCHES.gh (14.3 KB)

1 Like

Thanks so much for your help friends :slight_smile: