python - average from a dictionary (values) -


i'm trying change result if there 2 grades in values replace 2 grades average. tried many techniques failed. need write solution average , delete 2 values of grades.

i wrote code:

def mydict(grades, teachers):     dict={}     i1 in grades:         i2 in teachers:             key=i2[1]             value=[]              dict[key]=value #{'statistics': [], 'philosophy': [], 'computer': [], 'physics': [], 'english': []}             i1 in grades:                 if key==i1[-1]:                     value.append(i1[0]) #{'statistics': [23560, 23452], 'philosophy': [], 'computer': [23415, 12345], 'physics': [23452, 23459], 'english': [12345]}              i1 in grades:                 if key==i1[-1]:                     value.append(i1[1])              value_size=len(value)             if value_size>2:                 end=int(value_size)/2                 in value[-1:end]:                     print float(count(i)/value_size)      print dict  grades = [[12345,75,'english'],              [23452,83,'physics'],              [23560,81,'statistics'],              [23415,61,'computer'],              [23459,90,'physics'],                  [12345,75,'computer'],              [23452,100,'statistics']]  teachers = [['aharoni','english'],                ['melamed','physics'],                ['kaner','computer'],                ['zloti','statistics'],                ['korman','philosophy']]  print mydict(grades, teachers) 

the result is:

>>>  {'statistics': [23560, 23452, 81, 100], 'philosophy': [], 'computer': [23415, 12345, 61, 75], 'physics': [23452, 23459, 83, 90], 'english': [12345, 75]} none >>>  

what want (it in process, stuck in level):

{ 'aharoni': [12345, 75.0], 'kaner': [23415, 12345, 68.0], 'melamed':   [23452, 23459, 86.5], 'korman': [], 'zloti': [23560, 23452, 90.5] } 

what simple loop:

mydict = {}  teacher, subject in teachers:     values = []     scores = []     i1, i2, s in grades:         if subject == s:             values.append(i1)             scores.append(i2)      if scores:         average = sum(scores) / len(scores)         values.append(average)      mydict[teacher] = values 

first, iterate trough teachers, , each matching subject in grade list, append i1 , i2 list.

at end of iteration, can compute average of i2 values (if list not empty) , update dictionnary.


the output data be:

{     'korman': [],     'melamed': [23452, 23459, 86.5],     'zloti': [23560, 23452, 90.5],     'aharoni': [12345, 75.0],     'kaner': [23415, 12345, 68.0] } 

Comments