Python – 提取缺失范围
给定元组列表、开始范围和结束范围值,提取列表中缺少的范围。
Input : test_list = [(7, 2), (15, 19), (38, 50)], strt_val = 5, stop_val = 60
Output : [(5, 7), (2, 60), (2, 15), (19, 60), (19, 38), (50, 60)]
Explanation : Missing element ranges starting from 5 and ending at 50-60 are output as desired.
Input : test_list = [(7, 2), (15, 19), (38, 50)], strt_val = 1, stop_val = 52
Output : [(1, 7), (2, 60), (2, 15), (19, 60), (19, 38), (50, 52)]
Explanation : Similar as above, just ranges are altered.
方法:使用循环
这是可以执行此任务的蛮力方法。在此,我们跟踪开始和停止值,并不断添加缺失的范围。最后,检查是否有任何范围小于较高范围很重要。
# 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)]