简体   繁体   中英

average from a dictionary (values)

I'm trying to change the result so if there are 2 grades in values it will replace the 2 grades with the average. I tried so many techniques to do that but failed. I need to write a solution for the average and to delete the 2 values of the grades.

I wrote this code:

def myDict(grades, teachers):
    Dict={}
    for i1 in grades:
        for i2 in teachers:
            key=i2[1]
            value=[]

            Dict[key]=value #{'Statistics': [], 'Philosophy': [], 'Computer': [], 'Physics': [], 'English': []}
            for i1 in grades:
                if key==i1[-1]:
                    value.append(i1[0]) #{'Statistics': [23560, 23452], 'Philosophy': [], 'Computer': [23415, 12345], 'Physics': [23452, 23459], 'English': [12345]}

            for i1 in grades:
                if key==i1[-1]:
                    value.append(i1[1])

            value_size=len(value)
            if value_size>2:
                end=int(value_size)/2
                for i 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 i want to get (it is in process, i am stuck in this level):

{ 'Aharoni': [12345, 75.0], 'Kaner': [23415, 12345, 68.0], 'Melamed':   [23452, 23459, 86.5], 'Korman': [], 'Zloti': [23560, 23452, 90.5] }

What about this simple loop:

myDict = {}

for teacher, subject in teachers:
    values = []
    scores = []
    for 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 the teachers, and for each matching subject in the grade list, append i1 and i2 to some list.

At the end of the iteration, you can easily compute the average of i2 values (if the list is not empty) and then update your dictionnary.


The output with your data would be:

{
    'Korman': [],
    'Melamed': [23452, 23459, 86.5],
    'Zloti': [23560, 23452, 90.5],
    'Aharoni': [12345, 75.0],
    'Kaner': [23415, 12345, 68.0]
}

List comprehensions are a great way to deal with a data structure like that:

def myDict(grades, teachers):
    subjects = [x[1] for x in teachers]
    d = {}
    for s in subjects:
        subject_grades_records = [x for x in grades if x[2] == s]
        value = [x[0] for x in subject_grades_records]
        if len(value) > 0:
            value.append(sum(x[1] for x in subject_grades_records) / float(len(subject_grades_records)))
        teacher = [x[0] for x in teachers if x[1] == s][0]
        d[teacher] = value
    return d


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(repr(myDict(grades, teachers)))

# {'Kaner': [23415, 12345, 68.0], 'Aharoni': [12345, 75.0], 'Zloti': [23560, 23452, 90.5], 'Melamed': [23452, 23459, 86.5], 'Korman': []}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM