减去没有集合的两个范围之间的重叠 [英] Subtract Overlaps Between Two Ranges Without Sets
本文介绍了减去没有集合的两个范围之间的重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
没有设置!
我不能使用 Sets,因为:
I can't use Sets because:
- 范围会太长.
- 它们会占用太多内存
- 创建集合本身需要很长时间.
仅使用范围的端点,是否有最佳方法来减去两个范围列表?
Using only the endpoints of the of the ranges, is there an optimal way to subtract two lists of ranges?
r1 = (1, 1000), (1100, 1200)
r2 = (30, 50), (60, 200), (1150, 1300)
r1 - r2 = (1, 29), (51, 59), (201, 1000), (1100, 1149)
其他信息:
- r2 不必与 r1 重叠
- r1 和 r2 不会有与其他对重叠的对.例如,r1 不会同时具有 (0,30) 和 (10, 25)
谢谢.
推荐答案
interval 包可能提供您所需要的一切.
The interval package may provide all that you need.
from interval import Interval, IntervalSet
r1 = IntervalSet([Interval(1, 1000), Interval(1100, 1200)])
r2 = IntervalSet([Interval(30, 50), Interval(60, 200), Interval(1150, 1300)])
print(r1 - r2)
>>> [1..30),(50..60),(200..1000],[1100..1150)
这篇关于减去没有集合的两个范围之间的重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文