我正在尝试编写一些代码来计算两组间隔A - B之间的差异,间隔端点是整数,但我很有希望得到有效的解决方案,任何建议都会得到很多赞赏的例子:[(1,4) ),(7,9)] - [(3,5)] = [(1,3),(7,9)]
这是迄今为止我尝试过的最好的(两个列表已经排序)
class tp():
def __repr__(self):
return '(%.2f,%.2f)' % (self.start, self.end)
def __init__(self,start,end):
self.start=start
self.end=end
z=[tp(3,5)] #intervals to be subtracted
s=[tp(1, 4)),tp(7, 9), tp(3,4),tp(4,6)]
for x in s[:]:
if z.end < x.start:
break
elif z.start < x.start and z.end > x.start and z.end < x.end:
x.start=z.end
elif z.start < x.start and z.end > x.end:
s.remove(x)
elif z.start > x.start and z.end < x.end:
s.append(tp(x.start,z.start))
s.append(tp(z.end,x.end))
s.remove(x)
elif z.start > x.start and z.start < …Run Code Online (Sandbox Code Playgroud)