1

I am reading, processing and fitting some data that has been collected at different temperatures. For each set, the measured temperature oscillates a little so I have grouped the results by taking temperature averages. I then ask the user to select the temperatures of interest (discard noisy ones for instance) via raw_input, and use those indices to locate the data that will be fitted. The problem is that the float averaged numbers and those indicated by the user use different representations and therefore cannot be compared (see Python - round a float to 2 digits).

Below is an example of what my code does:

# 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()

Since their representation cannot be compared, my code gets stuck at this point always. Also, note that even if I don't round the av_T and

selected_T = [ 25.999885000000003, 30.000000357142863 ]

I get the same behaviour. Is there a way to make this comparison without resorting to decimal precision?

dani reta
  • 69
  • 8
  • If I understand your question correctly, you want your `rounded_T` array to be `[26.0, 28.0, 30.0, 32.0]`? You can do this by rounding to 1 place instead of 2. So it would be `[round(x,1) for x in av_T]`. – Vishnu Dasu Jun 04 '19 at 07:02

2 Answers2

0

Have a look at math library's isclose method

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.

You should be able to use the rel_tol (Relative tolerance) as per your requirements.

Optimus
  • 697
  • 2
  • 8
  • 22
0

Your conversion code is fine, the problem is that you cannot use in operator to compare if one list is contained in another list, since membership works by checking one item to be in a list or not.

To check if one list is contained within another, you can convert both lists to sets, and perform set.intersection on them

# 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()

This will not output anything since the if condition is not satisfied

Community
  • 1
  • 1
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40