ilra
ilra

Reputation: 145

Why am I getting this output when running function

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)

Upvotes: 0

Views: 56

Answers (1)

LTJ
LTJ

Reputation: 1255

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.

Upvotes: 1

Related Questions