平均字典(值)

时间:2016-03-26 12:39:33

标签: python list dictionary

我试图更改结果,因此如果值中有2个等级,它将用平均值替换2个等级。我尝试了很多技术但是失败了。 我需要为平均值编写解决方案并删除等级的2个值。

我写了这段代码:

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)

结果是:

>>> 
{'Statistics': [23560, 23452, 81, 100], 'Philosophy': [], 'Computer': [23415, 12345, 61, 75], 'Physics': [23452, 23459, 83, 90], 'English': [12345, 75]}
None
>>> 

我想得到什么(它正在进行中,我陷入了这个级别):

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

2 个答案:

答案 0 :(得分:1)

这个简单的循环怎么样:

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

首先,通过教师进行迭代,对于成绩列表中的每个匹配主题,将i1i2添加到某个列表中。

在迭代结束时,您可以轻松计算i2值的平均值(如果列表不为空),然后更新您的词典。

您的数据输出为:

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

答案 1 :(得分:1)

列表推导是处理数据结构的好方法:

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': []}