I'm writing a python function that gets a list of probabilities (each rounded to 2 decimal points) in case the sum(list)
is not equal to 1.0
and updates the first element of the list to make the sum(list) = 1.0
. The Function is below:
def round_off_list_to_1(prob_list):
if len(prob_list) != 0:
prob_list[0] = round(prob_list[0] + (1.0 - sum(prob_list)), 2)
assert sum(prob_list) == 1, "Sum of random probabilities not equal to 1.0"
return prob_list
What I don't understand is that for the list = [.59, .33, .08]
, sum(list) = .9999999
. The function gets called and the list remains the same and assert fails.