您正在描述间隔树。
pip install intervaltree
用法:
from intervaltree import IntervalTree, Intervaltree = IntervalTree()tree[200:400] = True # or you can use ranges as the "values"tree[450:470] = Truetree[700:900] = True
查询:
>>> treeIntervalTree([Interval(200, 400, True), Interval(450, 470, True), Interval(700, 900, True)])>>> tree[250]{Interval(200, 400, True)}>>> tree[150]set()
添加重叠范围:
>>> tree[450:490] = True>>> treeIntervalTree([Interval(200, 400, True), Interval(450, 470, True), Interval(450, 490, True), Interval(700, 900, True)])>>> tree.merge_overlaps()>>> treeIntervalTree([Interval(200, 400, True), Interval(450, 490), Interval(700, 900, True)])
丢弃:
>>> tree.chop(300, 350)>>> treeIntervalTree([Interval(200, 300, True), Interval(350, 400, True), Interval(450, 490), Interval(700, 900, True)])
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)