[英]How to check if the elements within a list of lists have overlapping numerical values
我正在處理一個列表列表,其中一些項目具有重疊的值。
list=[[7, 11, 'Feature01'],
[2, 6, 'Feature02'],
[31, 59, 'Feature03'],
[31, 41, 'Feature04'],
[20, 40, 'Feature05'],
[25, 30, 'Feature06']
例如,在以下項目中,Feature04 位於 Feature03 坐標內。
[31, 59, 'Feature03'], [31, 41, 'Feature04'],
同樣在下面的示例中,Feature06 位於 Feature05 坐標內。
[20, 40, 'Feature05'], [25, 30, 'Feature06']
我想在這種重疊場景中只保留一個項目並更新原始/主列表列表以保存非重疊列表項。
我發現幾乎類似的問題,但無法讓它發揮作用。
按區間的起點對列表進行排序,根據區間的端點進行平局(按降序排列)。 然后,如果最近添加的間隔不包含結果,則添加一個間隔:
lst.sort(key=lambda x: (x[0], -x[1]))
result = [lst[0]]
for entry in lst[1:]:
current_start, current_end, _ = entry
last_start, last_end, _ = result[-1]
if not (last_start <= current_start <= current_end <= last_end):
result.append(entry)
print(result)
這輸出:
[[2, 6, 'Feature02'], [7, 11, 'Feature01'], [20, 40, 'Feature05'], [31, 59, 'Feature03']]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.