gpt4 book ai didi

python - 将舍入列表与用户提供的列表进行比较

转载 作者:太空宇宙 更新时间:2023-11-03 20:48:02 24 4
gpt4 key购买 nike

我正在读取、处理和拟合一些在不同温度下收集的数据。对于每组,测量的温度都会略有波动,因此我通过取温度平均值对结果进行分组。然后,我要求用户通过 raw_input 选择感兴趣的温度(例如丢弃嘈杂的温度),并使用这些索引来定位要拟合的数据。问题是浮点平均数和用户指示的数使用不同的表示形式,因此无法进行比较(请参阅 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 ]

我也有同样的行为。有没有办法在不诉诸小数精度的情况下进行这种比较?

最佳答案

您的转换代码很好,问题是您不能使用 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 条件,因此不会输出任何内容

关于python - 将舍入列表与用户提供的列表进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56438814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com