Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以优雅的方式看待+;/-Python中围绕特定值的范围_Python_Python 3.x_Openpyxl - Fatal编程技术网

以优雅的方式看待+;/-Python中围绕特定值的范围

以优雅的方式看待+;/-Python中围绕特定值的范围,python,python-3.x,openpyxl,Python,Python 3.x,Openpyxl,我正在使用openpyxl库处理电子表格中的大量数据 我需要找到特定的温度值,然后根据该温度查看其他单元格 问题是我的体温测量值有点波动,但我真的不在乎这个 例如,如果我想在25度时查看数据,我真正想要的是24-26度范围内的数据。我需要在很多温度下这样做 我知道如何以一种相当混乱的迭代方式进行此操作,如下所示: for num in [5,10,15,20,25]: if temp > num -1 and temp < num + 1:

我正在使用openpyxl库处理电子表格中的大量数据

我需要找到特定的温度值,然后根据该温度查看其他单元格

问题是我的体温测量值有点波动,但我真的不在乎这个

例如,如果我想在25度时查看数据,我真正想要的是24-26度范围内的数据。我需要在很多温度下这样做

我知道如何以一种相当混乱的迭代方式进行此操作,如下所示:

for num in [5,10,15,20,25]:     
    if temp > num -1 and temp < num + 1:        
        #do things
for num in [5,10,15,20,25]:     
    if num in range(24,27):
        ...
for num in [5,10,15,20,25]:     
    if num - 1 <= temp <= num + 1:        
        #do things
range_comparison(operator.lt, [25, True], 24) # returns True
range_comparison(operator.lt, [25, True], 26) # returns None
[5,10,15,20,25]中的num的

如果温度>数值-1且温度<数值+1:
#做事

但这让我觉得很混乱,有没有更干净的方法呢?类似于检查temp是否在num的某个错误范围内?

您可以测试如下范围内的值:

for num in [5,10,15,20,25]:     
    if temp > num -1 and temp < num + 1:        
        #do things
for num in [5,10,15,20,25]:     
    if num in range(24,27):
        ...
for num in [5,10,15,20,25]:     
    if num - 1 <= temp <= num + 1:        
        #do things
range_comparison(operator.lt, [25, True], 24) # returns True
range_comparison(operator.lt, [25, True], 26) # returns None
请注意,如果传入的数据是浮动的,则这不起作用。 当然,您可以使用一些函数生成此范围,这些函数提供了一个中心和与中心的最大距离:

def s_range(center, max_distance): # surrounding range
    return range(center-max_distance, center+max_distance)
...
if num in s_range(25, 1):
    ...

你现在所拥有的是清楚的;Python允许您像这样链接比较:

for num in [5,10,15,20,25]:     
    if temp > num -1 and temp < num + 1:        
        #do things
for num in [5,10,15,20,25]:     
    if num in range(24,27):
        ...
for num in [5,10,15,20,25]:     
    if num - 1 <= temp <= num + 1:        
        #do things
range_comparison(operator.lt, [25, True], 24) # returns True
range_comparison(operator.lt, [25, True], 26) # returns None
[5,10,15,20,25]中的num的


如果num-1如果您只是想调整一下现有的内容:

for num in range(5, 30, 5):
    if abs(temp - num) < 1:
        # do things
用于范围(5,30,5)中的num:
如果abs(温度-数值)<1:
#做事

对于类似的内容,我编写了一个可以在此处轻松扩展使用的。尽管在这种情况下这可能有些过分,但如果您有许多具有不同值的类似检查,这是非常有意义的

像这样使用它:

for num in [5,10,15,20,25]:     
    if temp > num -1 and temp < num + 1:        
        #do things
for num in [5,10,15,20,25]:     
    if num in range(24,27):
        ...
for num in [5,10,15,20,25]:     
    if num - 1 <= temp <= num + 1:        
        #do things
range_comparison(operator.lt, [25, True], 24) # returns True
range_comparison(operator.lt, [25, True], 26) # returns None

您可以将此功能与自己的功能结合起来,以获得更大的灵活性。

内置解决方案如何?您只需使用中的函数即可(从
Python 3.5开始提供):


abs\u tol
表示绝对公差(即差),以便将两个数字视为接近,并
isclose
返回
True

对于希望在Python 2.x中使用内置函数的人,还有

从文件中:

对于有限值,isclose使用以下等式来测试 两个浮点值是等价的

absolute(a - b) <= (atol + rtol * absolute(b))
绝对值(a-b)