[英]How to check if range is partially within another range python?
Not the best wording, but I'll try to explain.不是最好的措辞,但我会尽力解释。
I have 4 regions, 0 <= x < 10
, 10 <= x < 30
, 30 <= x < 50
, 50 <= x < 90
.我有 4 个区域, 0 <= x < 10
、 10 <= x < 30
、 30 <= x < 50
、 50 <= x < 90
。
I'm writing a function which takes in x_min
and x_max
, and outputs the regions covered.我正在写一个 function ,它接受x_min
和x_max
,并输出覆盖的区域。 One of the edge cases that I can't solve in a "good" way is what if the range spans across multiple regions, eg x_min = 15
, x_max = 60
, where the output would be:我无法以“好的”方式解决的边缘情况之一是,如果范围跨越多个区域,例如x_min = 15
, x_max = 60
,其中 output 将是:
output = {'region1' : False,
'region2' : True,
'region3' : True,
'region4' : True}
My approach is below, the best solution I can come up with right now is to split the interval between x_min
and x_max
into 10, and check every number, but that adds 10 more loops (so for the x_min = 15
, x_max = 60
example, it would involve checking 15, 19.5, 24 and so on until 60).我的方法如下,我现在能想到的最佳解决方案是将x_min
和x_max
之间的间隔分成 10,并检查每个数字,但这会增加 10 个循环(因此对于x_min = 15
, x_max = 60
示例,它将涉及检查 15、19.5、24 等直到 60)。
Is there a better and more scalable way to do this?有没有更好、更具可扩展性的方法来做到这一点? I'm not sure which tools are available other than countless if - elif
statements in a for
loop.除了for
循环中的无数if - elif
语句之外,我不确定还有哪些工具可用。
def assign_regions(x_min, x_max):
values = [x_min, x_max]
output = {'region1' : False, # a dictionary to store the output
'region2' : False,
'region3' : False,
'region4' : False}
for x in values:
if 0 <= x < 10:
output['region1'] = True
elif 10 <= x < 30:
output['region2'] = True
elif ........
To test if two ranges overlap you only have to check that each range's start is less than the other range's end.要测试两个范围是否重叠,您只需检查每个范围的开始是否小于另一个范围的结束。
regions = { 'region1' : (0,10),
'region2' : (10,30),
'region3' : (30,50),
'region4' : (50,90)}
x_min,x_max = 15,60
overlaps = { reg:x_min < r_max and r_min < x_max
for reg,(r_min,r_max) in regions.items() }
print(overlaps)
# {'region1': False, 'region2': True, 'region3': True, 'region4': True}
You can store the ranges of each region and then check them in a loop:您可以存储每个区域的范围,然后循环检查它们:
values = [x_min, x_max]
region_info = {
'region1': range(0, 10),
'region2': range(10, 30),
# and so on
}
output = {region: False for region in region_info}
for x in values:
for region_name, its_range in region_info.items():
if x in its_range:
output[region_name] = True
Basically, you can replace if
statements with some data ( region_info
) and a loop over it.基本上,您可以用一些数据( region_info
)和循环替换if
语句。
I'd do something like:我会做类似的事情:
Store your ranges in a dict:将您的范围存储在字典中:
ranges = {'region1' : {'min':0, 'max': 10},
'region2' : {'min':10, 'max': 30},
'region3' : {'min':30, 'max': 50},
'region4' : {'min':50, 'max': 90}}
Iterate through dict and make comparisons:遍历 dict 并进行比较:
def assign_regions(x_min, x_max):
return {region: x_max>range_['min'] and x_min<range_['max']
for region, range_
in ranges.items()}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.