List Trouble - Python/Grasshopper

Hi there - I’m hoping to get some help on a script that’s been causing me issues. I’ve been trying to group a list of values into 20 or so groups based on a range of values for each group. For example, let’s say I have a list of values 1-100 and I am assigning each to one of 5 groups. Each group is equally spaced along the interval of discrete values so for this example, if I have 5 groups for numbers 1-100, each group would be an interval of 20 (1-20, 21-40, 41-60, etc.). For each discrete value I’m investigating, I want to return the group it belongs to so if the number is 45, I want to return 3. For some reason, I’m getting a weird nested list when I return. Can anyone help out with this? I’m still pretty new to grasshopper so any help would be appreciated!

I’ve attached some images showing the input and output as well as the python script.


image


ItemAccess (Simpler):

from Rhino.Geometry import Interval
step = (end - start) / count
for i in range(count):
    interval = Interval(start, start + step)
    start += step
    if interval.IncludesParameter(value):
        a = i
        break

ListAccess (Faster):

from Rhino.Geometry import Interval
step = (end - start) / count
intervals = []
for i in range(count):
    intervals.append(Interval(start, start + step))
    start += step
a = []
for v in values:
    for i in range(len(intervals)):
        if intervals[i].IncludesParameter(v):
            a.append(i)
            break

Intervals.gh (8.9 KB)

2 Likes

Hi Mahdiyar! Thank you so much for this great solution! It worked so well!