0

I'm trying to calculate the mean value of the last numbers of this measurements(those belonging to the same batch/first number)

1,  73
1,  101
2,  17
2,  23

for example I want my code to return 2 mean values for the values above(desired output):

1    87.0.       #(73+101)/2
2    20.0        #(17+23)/2

But somehow my code is only returning one value, can't really find the problem, would be appreciated if someone can tell me what the problem is! this is my code!

def collect_exp_data(file_name):
    data = dict()
    with open(file_name, 'r') as h:
        for line in h:
            batch, x, y, value = line.split(',')
            print(batch)
            if not batch in data:
                data[batch] = []
            data[batch] += [(float(x),float(y),float(value))]
    return data
    
def value_average(dict):
    for batch, sample in dict.items():
        average_list_length = 0
        average_list_sum = 0
        for x,y,value in sample: 
            if x**2 + y**2 <= 1:
                average_list_sum += value
                average_list_length += 1
        average = average_list_sum/average_list_length
        return batch, average 
   
for batch, sample in collect_exp_data("/Users/../Desktop/sample1.txt").items():          
            print(value_average(collect_exp_data("/Users/../Desktop/sample1.txt")))

this is what im getting back

('1', 87.0)
ilra
  • 149
  • 6
  • https://stackoverflow.com/questions/36810595/calculate-average-of-every-x-rows-in-a-table-and-create-new-table it may help you – Bhar Jay Sep 20 '22 at 10:35
  • Unfortunately it did not since i'm trying to write the code by myself without using imports! – ilra Sep 20 '22 at 10:38

1 Answers1

1

You seem to be running the main loop twice, first in the end of the file with for batch, sample in collect_exp_data(... and second time in value_average function. Only one of these is required. Here is a modified example which produces a dictionary with your requested values:

def value_average(data):
    return_dict = {}
    for batch, sample in data.items():
        average_list_length = 0
        average_list_sum = 0
        for x, y, value in sample: 
            if x**2 + y**2 <= 1:
                average_list_sum += value
                average_list_length += 1
        average = average_list_sum/average_list_length
        return_dict[batch] = average
    return return_dict

print(value_average(collect_exp_data("/Users/../Desktop/sample1.txt")))

Also, refrain from using dict, list or other built-in python types as a variable name, it will most likely mess some code up later on and is a bad practice.

LTJ
  • 1,197
  • 3
  • 16
  • Thank you your answer! Your feedback helps very much since im a beginner and trying to improve my code as much as I can! Can you maybe spot other things that I can improve so the code becomes easier to read and understand? Maybe unnecessary steps in the code? – ilra Sep 20 '22 at 10:56
  • 1
    @PatrickArtner My bad, I'll modify the answer. You're absolutely right it works as is and the loop implementation was the issue. – LTJ Sep 20 '22 at 10:59