如果您的间隔是a, b和c, d,即
(a, b), (c, d) = [10, 20], [15, 20]
Run Code Online (Sandbox Code Playgroud)
然后重叠间隔是
x, y = max(a, c), min(b, d)
if x > y: # no overlap
x, y = None, None
Run Code Online (Sandbox Code Playgroud)
并且重叠量是y - x或y - x + 1,取决于您的间隔是关闭还是半关闭(假设这里是整数).