将舍入列表与用户提供的列表进行比较
Comparing a rounded list with user provided list
我正在读取、处理和拟合一些在不同温度下收集的数据。对于每组,测得的温度都会有一点波动,所以我通过取温度平均值对结果进行分组。然后,我通过 raw_input 要求用户 select 感兴趣的温度(例如,丢弃嘈杂的温度),并使用这些索引来定位将要拟合的数据。问题是浮动平均数和用户指示的数字使用不同的表示法,因此无法进行比较(参见Python - round a float to 2 digits)。
下面是我的代码的一个例子:
# Example of temperatures on my data set
T = [25.99545, 25.99702, 25.9982, 25.99859, 25.9986, 25.99899, 25.99899, 25.99899, 25.99899, 25.99899, 25.99899, 25.99938, 25.99938, 26.00016, 26.00056, 26.00056, 26.00056, 26.00056, 26.00056, 26.00095, 26.00095, 26.00095, 26.00134, 26.00134, 26.00134, 26.00174, 26.00213, 26.00252, 27.998, 27.99846, 27.99846, 27.99891, 27.99891, 27.99891, 27.99935, 27.99935, 27.99936, 27.9998, 27.9998, 27.9998, 27.99981, 27.99981, 28.00025, 28.00025, 28.00026, 28.00026, 28.0007, 28.0007, 28.0007, 28.0007, 28.00114, 28.00115, 28.00115, 28.00115, 28.00204, 28.00249, 29.99771, 29.99822, 29.99822, 29.99822, 29.99873, 29.99873, 29.99873, 29.99923, 29.99923, 29.99924, 29.99974, 29.99974, 29.99975, 29.99975, 29.99975, 30.00026, 30.00026, 30.00026, 30.00026, 30.00076, 30.00076, 30.00127, 30.00127, 30.00178, 30.00178, 30.00178, 30.00229, 30.00229, 31.99801, 31.99858, 31.99858, 31.99858, 31.99858, 31.99858, 31.99916, 31.99916, 31.99916, 31.99916, 31.99973, 32.00029, 32.0003, 32.0003, 32.0003, 32.0003, 32.0003, 32.00086, 32.00086, 32.00087, 32.00087, 32.00143, 32.00143, 32.00143, 32.002, 32.00201, 32.00257, 32.00372 ]
av_T = [ 25.999885000000003, 28.000059642857156, 30.000000357142863, 32.000254285714284 ] # Average of temperatures
rounded_T = [ round(x,2) for x in av_T ]
selected_T = [ 26.0, 30.0 ] # User selection of temperatures
if selected_T not in rounded_T: # Check the user indicates valid temperatures
print('The selected temperature is not in your experimental set')
exit()
由于无法比较它们的表示,所以我的代码总是卡在这一点上。
另外,请注意,即使我不舍入 av_T 和
selected_T = [ 25.999885000000003, 30.000000357142863 ]
我也有同样的行为。
有没有办法在不求助于小数精度的情况下进行这种比较?
看看 math 库的 isclose
方法
math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
Return True if the values a and b are close to each other and False
otherwise.
您应该能够根据您的要求使用 rel_tol(相对容差)。
您的转换代码很好,问题是您不能使用 in
运算符来比较一个列表是否包含在另一个列表中,因为成员资格的工作原理是检查一个项目是否在列表中。
要检查一个列表是否包含在另一个列表中,您可以将两个列表都转换为集合,然后对它们执行 set.intersection
# Example of temperatures on my data set
T = [25.99545, 25.99702, 25.9982, 25.99859, 25.9986, 25.99899, 25.99899, 25.99899, 25.99899, 25.99899, 25.99899, 25.99938, 25.99938, 26.00016, 26.00056, 26.00056, 26.00056, 26.00056, 26.00056, 26.00095, 26.00095, 26.00095, 26.00134, 26.00134, 26.00134, 26.00174, 26.00213, 26.00252, 27.998, 27.99846, 27.99846, 27.99891, 27.99891, 27.99891, 27.99935, 27.99935, 27.99936, 27.9998, 27.9998, 27.9998, 27.99981, 27.99981, 28.00025, 28.00025, 28.00026, 28.00026, 28.0007, 28.0007, 28.0007, 28.0007, 28.00114, 28.00115, 28.00115, 28.00115, 28.00204, 28.00249, 29.99771, 29.99822, 29.99822, 29.99822, 29.99873, 29.99873, 29.99873, 29.99923, 29.99923, 29.99924, 29.99974, 29.99974, 29.99975, 29.99975, 29.99975, 30.00026, 30.00026, 30.00026, 30.00026, 30.00076, 30.00076, 30.00127, 30.00127, 30.00178, 30.00178, 30.00178, 30.00229, 30.00229, 31.99801, 31.99858, 31.99858, 31.99858, 31.99858, 31.99858, 31.99916, 31.99916, 31.99916, 31.99916, 31.99973, 32.00029, 32.0003, 32.0003, 32.0003, 32.0003, 32.0003, 32.00086, 32.00086, 32.00087, 32.00087, 32.00143, 32.00143, 32.00143, 32.002, 32.00201, 32.00257, 32.00372 ]
av_T = [ 25.999885000000003, 28.000059642857156, 30.000000357142863, 32.000254285714284 ] # Average of temperatures
rounded_T = [ round(x,2) for x in av_T ]
selected_T = [ 26.0, 30.0 ] # User selection of temperatures
#Get common elements
common_elems = set(selected_T).intersection(set(rounded_T))
#Check if common elements exist by using the fact that empty sets evaluate to False
if not common_elems:
print('The selected temperature is not in your experimental set')
exit()
由于不满足 if 条件,因此不会输出任何内容
我正在读取、处理和拟合一些在不同温度下收集的数据。对于每组,测得的温度都会有一点波动,所以我通过取温度平均值对结果进行分组。然后,我通过 raw_input 要求用户 select 感兴趣的温度(例如,丢弃嘈杂的温度),并使用这些索引来定位将要拟合的数据。问题是浮动平均数和用户指示的数字使用不同的表示法,因此无法进行比较(参见Python - round a float to 2 digits)。
下面是我的代码的一个例子:
# Example of temperatures on my data set
T = [25.99545, 25.99702, 25.9982, 25.99859, 25.9986, 25.99899, 25.99899, 25.99899, 25.99899, 25.99899, 25.99899, 25.99938, 25.99938, 26.00016, 26.00056, 26.00056, 26.00056, 26.00056, 26.00056, 26.00095, 26.00095, 26.00095, 26.00134, 26.00134, 26.00134, 26.00174, 26.00213, 26.00252, 27.998, 27.99846, 27.99846, 27.99891, 27.99891, 27.99891, 27.99935, 27.99935, 27.99936, 27.9998, 27.9998, 27.9998, 27.99981, 27.99981, 28.00025, 28.00025, 28.00026, 28.00026, 28.0007, 28.0007, 28.0007, 28.0007, 28.00114, 28.00115, 28.00115, 28.00115, 28.00204, 28.00249, 29.99771, 29.99822, 29.99822, 29.99822, 29.99873, 29.99873, 29.99873, 29.99923, 29.99923, 29.99924, 29.99974, 29.99974, 29.99975, 29.99975, 29.99975, 30.00026, 30.00026, 30.00026, 30.00026, 30.00076, 30.00076, 30.00127, 30.00127, 30.00178, 30.00178, 30.00178, 30.00229, 30.00229, 31.99801, 31.99858, 31.99858, 31.99858, 31.99858, 31.99858, 31.99916, 31.99916, 31.99916, 31.99916, 31.99973, 32.00029, 32.0003, 32.0003, 32.0003, 32.0003, 32.0003, 32.00086, 32.00086, 32.00087, 32.00087, 32.00143, 32.00143, 32.00143, 32.002, 32.00201, 32.00257, 32.00372 ]
av_T = [ 25.999885000000003, 28.000059642857156, 30.000000357142863, 32.000254285714284 ] # Average of temperatures
rounded_T = [ round(x,2) for x in av_T ]
selected_T = [ 26.0, 30.0 ] # User selection of temperatures
if selected_T not in rounded_T: # Check the user indicates valid temperatures
print('The selected temperature is not in your experimental set')
exit()
由于无法比较它们的表示,所以我的代码总是卡在这一点上。 另外,请注意,即使我不舍入 av_T 和
selected_T = [ 25.999885000000003, 30.000000357142863 ]
我也有同样的行为。 有没有办法在不求助于小数精度的情况下进行这种比较?
看看 math 库的 isclose
方法
math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
Return True if the values a and b are close to each other and False otherwise.
您应该能够根据您的要求使用 rel_tol(相对容差)。
您的转换代码很好,问题是您不能使用 in
运算符来比较一个列表是否包含在另一个列表中,因为成员资格的工作原理是检查一个项目是否在列表中。
要检查一个列表是否包含在另一个列表中,您可以将两个列表都转换为集合,然后对它们执行 set.intersection
# Example of temperatures on my data set
T = [25.99545, 25.99702, 25.9982, 25.99859, 25.9986, 25.99899, 25.99899, 25.99899, 25.99899, 25.99899, 25.99899, 25.99938, 25.99938, 26.00016, 26.00056, 26.00056, 26.00056, 26.00056, 26.00056, 26.00095, 26.00095, 26.00095, 26.00134, 26.00134, 26.00134, 26.00174, 26.00213, 26.00252, 27.998, 27.99846, 27.99846, 27.99891, 27.99891, 27.99891, 27.99935, 27.99935, 27.99936, 27.9998, 27.9998, 27.9998, 27.99981, 27.99981, 28.00025, 28.00025, 28.00026, 28.00026, 28.0007, 28.0007, 28.0007, 28.0007, 28.00114, 28.00115, 28.00115, 28.00115, 28.00204, 28.00249, 29.99771, 29.99822, 29.99822, 29.99822, 29.99873, 29.99873, 29.99873, 29.99923, 29.99923, 29.99924, 29.99974, 29.99974, 29.99975, 29.99975, 29.99975, 30.00026, 30.00026, 30.00026, 30.00026, 30.00076, 30.00076, 30.00127, 30.00127, 30.00178, 30.00178, 30.00178, 30.00229, 30.00229, 31.99801, 31.99858, 31.99858, 31.99858, 31.99858, 31.99858, 31.99916, 31.99916, 31.99916, 31.99916, 31.99973, 32.00029, 32.0003, 32.0003, 32.0003, 32.0003, 32.0003, 32.00086, 32.00086, 32.00087, 32.00087, 32.00143, 32.00143, 32.00143, 32.002, 32.00201, 32.00257, 32.00372 ]
av_T = [ 25.999885000000003, 28.000059642857156, 30.000000357142863, 32.000254285714284 ] # Average of temperatures
rounded_T = [ round(x,2) for x in av_T ]
selected_T = [ 26.0, 30.0 ] # User selection of temperatures
#Get common elements
common_elems = set(selected_T).intersection(set(rounded_T))
#Check if common elements exist by using the fact that empty sets evaluate to False
if not common_elems:
print('The selected temperature is not in your experimental set')
exit()
由于不满足 if 条件,因此不会输出任何内容