📜  Python – 提取缺失范围

📅  最后修改于: 2022-05-13 01:55:36.294000             🧑  作者: Mango

Python – 提取缺失范围

给定元组列表、开始范围和结束范围值,提取列表中缺少的范围。

方法:使用循环
这是可以执行此任务的蛮力方法。在此,我们跟踪开始和停止值,并不断添加缺失的范围。最后,检查是否有任何范围小于较高范围很重要。

# Python3 code to demonstrate working of 
# Extract Missing Ranges
# Using loop
  
# initializing lists
test_list = [(6, 9), (15, 34), (48, 70)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing start val 
strt_val = 2
  
# initializing stop val 
stop_val = 100
  
# Using loop
res = []
for sub in test_list:
      
    # checking for start range
    if sub[0] > strt_val:
        res.append((strt_val, sub[0]))
        strt_val = sub[1]
  
    # checking for end range
    if strt_val < stop_val:
        res.append((strt_val, stop_val))
  
# printing result 
print("Missing range tuples : " + str(res)) 
输出 :
The original list is : [(6, 9), (15, 34), (48, 70)]
Missing range tuples : [(2, 6), (9, 100), (9, 15), (34, 100), (34, 48), (70, 100)]