Given a shortish sequence of thresholds ordered ascendantly and numerous values.
Wanted result is a sequence of set
s, first one containing all distinct values below the lowest/first threshold; next values not below lowest threshold, but below 2nd threshold, if any; and so on 'til last threshold; finally all values not below highest threshold.
There are similar questions about dict
s (pointers to helpful solutions there welcome, too),
suggestions amounting to
from itertools import pairwise
def partition(values, thresholds):
""" Partition values into a list of sets
with values in right-open intervals specified by thresholds.
"""
return [ { v for v in values if v < thresholds[0] }
] + [ { v for v in values if lo <= v < hi }
for lo, hi in tuple(pairwise(thresholds))
] + [ { v for v in values if thresholds[-1] <= v } ]
This "iterates" values
len(thresholds)+1
times.
How to efficiently create a sequence of set
s partitioning values according to thresholds?
from Efficient creation of a sequence of sets from values and thresholds
No comments:
Post a Comment