pon*_*cat 2 python search tuples list overlap
假设我有一个完整的元组列表,表示"从"和"到"时间:
tuples = [ (0, 5), (5, 10), (10, 15), (15,20) ]
Run Code Online (Sandbox Code Playgroud)
我希望能够检索与给定元组重叠的元组列表:
searchTuple = (3,11)
result = findOverlap(tuples, searchTuple)
Run Code Online (Sandbox Code Playgroud)
此代码应返回以下列表:
[ (0, 5), (5, 10), (10, 15) ]
Run Code Online (Sandbox Code Playgroud)
而(16,22)的searchTuple应该只返回最后一个元组(15,20)
编码此检索的最有效方法是什么?我尝试了各种各样的东西,但我无法让算法正常工作.我想到了以下不同的"重叠",我有兴趣捕捉:
a) tuple_min < find_min AND tuple_max > find_max
search tuple -> | |
|----------------| the search tuple is entirely contained
b) tuple_min > find_min AND tuple_max > find_max
| |
|----------------| the left part of the tuple overlaps
c) tuple_min < find_min AND tuple_max < find_max
| |
|----------------| the right part of the tuple overlaps
Run Code Online (Sandbox Code Playgroud)
然而,我在实施这个结果后得到的结果最终给了我错误的结果......我的想法在哪里错了?