参考链接:https://pypi.org/project/intervaltree/
安装:
pip install intervaltree
或者参考:
https://anaconda.org/conda-forge/intervaltree
conda install conda-forge::intervaltree
conda install conda-forge/label/cf201901::intervaltree
conda install conda-forge/label/cf202003::intervaltree
conda install conda-forge/label/gcc7::intervaltree
首先介绍Intervel
from intervaltree import Interval, IntervalTree
iv = Interval(4,6,(4,6))
print(iv.begin)
print(iv.end)
print(iv.data)
begin, end , data = iv
print(begin)
print(end)
print(data)
这个工具用于标记文本和时间的范围,其中包括下限不包括上限
关于初始化
Initializing
blank tree = IntervalTree()
from an iterable of Interval objects (tree = IntervalTree(intervals))
from an iterable of tuples (tree = IntervalTree.from_tuples(interval_tuples))
创建一个空白的IntervalTree
from intervaltree import Interval, IntervalTree
t = IntervalTree()
t
关于插入
t = IntervalTree()
t[1:2] = "1-2"
t[4:7] = (4, 7)
t[5:9] = {5: 9}
print(t)
###增加
test1 = Interval(100,200, "test1")
print(test1)
t.add(test1)
print(t)
##使用.addi直接添加
t.addi(30,40,"test2")
t.addi(1000, 2000, "test3")
print(t)
##删除
IntervalTree([Interval(1, 2, '1-2'), Interval(4, 7, (4, 7)), Interval(5, 9, {5: 9})])
Interval(100, 200, 'test1')
IntervalTree([Interval(1, 2, '1-2'), Interval(4, 7, (4, 7)), Interval(5, 9, {5: 9}), Interval(100, 200, 'test1')])
IntervalTree([Interval(1, 2, '1-2'), Interval(4, 7, (4, 7)), Interval(5, 9, {5: 9}), Interval(30, 40, 'test2'), Interval(100, 200, 'test1'), Interval(1000, 2000, 'test3')])
关于删除
Deletions
tree.remove(interval) (raises ValueError if not present)
tree.discard(interval) (quiet if not present)
tree.removei(begin, end, data) (short for tree.remove(Interval(begin, end, data)))
tree.discardi(begin, end, data) (short for tree.discard(Interval(begin, end, data)))
tree.remove_overlap(point)
tree.remove_overlap(begin, end) (removes all overlapping the range)
tree.remove_envelop(begin, end) (removes all enveloped in the range)
print("初始状态", t)
t.remove(Interval(1, 2, '1-2'))
print("第一次删除", t)
t.discard(Interval(4, 7, (4, 7)))
print("第二次删除", t)
#使用.removei以及.discardi简化删除
t.removei(5, 9, {5: 9})
print("第三次删除",t )
###使用.remove_overlap()根据点的位置删除
t.remove_overlap(35)
print("第四次删除", t )
###使用.remove_overlap(begin, end) 根据区域是否重叠删除
t.remove_overlap(50,101)
print("第五次删除", t)
raw_data IntervalTree([Interval(1, 2, '1-2'), Interval(4, 7, (4, 7)), Interval(5, 9, {5: 9}), Interval(30, 40, 'test2'), Interval(100, 200, 'test1')])
第一次删除 IntervalTree([Interval(4, 7, (4, 7)), Interval(5, 9, {5: 9}), Interval(30, 40, 'test2'), Interval(100, 200, 'test1')])
第二次删除 IntervalTree([Interval(5, 9, {5: 9}), Interval(30, 40, 'test2'), Interval(100, 200, 'test1')])
第三次删除 IntervalTree([Interval(30, 40, 'test2'), Interval(100, 200, 'test1')])
第四次删除 IntervalTree([Interval(100, 200, 'test1')])
第五次删除 IntervalTree()
根据位置(点或者区域信息)查找区域
##根据点查找
##.search有的版本中已经被替换了
tree = IntervalTree()
tree.addi(10,30,'test1')
tree.add(Interval(20,40, "test2"))
print(tree)
print("由点找区域", tree[20])
print("由点找区域", tree.at(25))
print("由点找区域",tree[100])
print("寻找交集区域",tree.overlap(25,30))
print("寻找包含的区域", tree.envelop(10,15))
print("寻找包含的区域", tree.envelop(10,100))
IntervalTree([Interval(10, 30, 'test1'), Interval(20, 40, 'test2')])
由点找区域 {Interval(20, 40, 'test2'), Interval(10, 30, 'test1')}
由点找区域 {Interval(20, 40, 'test2'), Interval(10, 30, 'test1')}
由点找区域 set()
寻找交集区域 {Interval(20, 40, 'test2'), Interval(10, 30, 'test1')}
寻找包含的区域 set()
寻找包含的区域 {Interval(20, 40, 'test2'), Interval(10, 30, 'test1')}
判断所属关系
#判断所属关系
print(tree)
print(Interval(10, 30, 'test1') in tree)
print(tree.overlaps(30))
print(tree.containsi(20,25))
print(tree.containsi(10,30))
print(tree.containsi(10,30, "test1"))
合并
# 创建第一个 IntervalTree 对象
tree1 = IntervalTree()
tree1.addi(10, 20)
tree1.addi(30, 40)
# 创建第二个 IntervalTree 对象
tree2 = IntervalTree()
tree2.addi(15, 25)
tree2.addi(35, 45)
# 将两个 IntervalTree 对象合并
result_tree = tree1.union(tree2)
print("合并方法一",result_tree)
result_tree = tree1 | tree2
print("合并方法二",result_tree)
result_tree = tree1.update(tree2)
print("合并方法三", result_tree)
tree1 |= tree2
print("合并方法四",tree1)
合并方法一 IntervalTree([Interval(10, 20), Interval(15, 25), Interval(30, 40), Interval(35, 45)])
合并方法二 IntervalTree([Interval(10, 20), Interval(15, 25), Interval(30, 40), Interval(35, 45)])
合并方法三 None
合并方法四 IntervalTree([Interval(10, 20), Interval(15, 25), Interval(30, 40), Interval(35, 45)])
###第三种不知道为啥没成
切分区域 slicing
t = IntervalTree([Interval(0, 10), Interval(5, 15), Interval(0,13)])
t.slice(3)
IntervalTree([Interval(0, 3), Interval(3, 10), Interval(3, 13), Interval(5, 15)])
我理解为砍掉一部分区域
t = IntervalTree([Interval(0, 10), Interval(5,8)])
t.chop(3, 7)
IntervalTree([Interval(0, 3), Interval(7, 8), Interval(7, 10)])
##就把3到7的区域都砍了