如何找出字典中分配给键的值的平均值?

时间:2015-01-29 16:52:52

标签: python dictionary

我已经制作了一些代码,它将测试分数的结果从文本文件添加到python字典中,格式为:

{'Ruan': '22', 'hello': [22, 1], 'kurun': '29'}

我想弄清楚每个人得分的平均值,这是我到目前为止所尝试的:

while choice == 'av':
    if schClass == '1':
     schClass = open("scores1.txt", 'r')
     li = open("scores1.txt", 'r')
     data = li.read().splitlines()
     for li in data:
        name = li.split(":")[0]
        score = li.split(":")[1]
        if name not in diction1:
            diction1[name] = score
        if name in diction1:
                    diction1[name] = [int(diction1[name]),int(score)]       
        print(diction1)
        averages_dct = {}
        for name in diction1:
            student_average = sum((diction1[name])) / len((diction1[name]))
            averages_dct.update({name: student_average})
        reversed_dct = {averages_dct[k]: [] for k in averages_dct}
        for average in reversed_dct:
            for name in averages_dct:
                if average == averages_dct[name]:
                           reversed_dct[average].append(name)
                           for av in sorted(reversed_dct, reverse=True):
                               print('average: %s, students: %s' % (av, reversed_dct[av]))

这是错误:

    student_average = sum((diction1[name])) / len((diction1[name]))
TypeError: unsupported operand type(s) for +: 'int' and 'str'

我很清楚这完全意味着什么,不知道如何解决它?

2 个答案:

答案 0 :(得分:0)

在数据结构中混合使用字符串和整数列表是不明智的。你应该尝试类似的东西。这有助于进一步计算:

while choice == 'av':
    if schClass == '1':
     schClass = open("scores1.txt", 'r')
     li = open("scores1.txt", 'r')
     data = li.read().splitlines()
     for li in data:
        name = li.split(":")[0]
        score = li.split(":")[1]

        diction1.setdefault(name,[]).append(int(score))

     # The following loop should work,
     # even if it can be optimized (see Padraic's answer)
     for name in diction1:
        student_average = sum(diction1[name]) / len(diction1[name])
        averages_dct[name] = student_average
     ...

设置setdefault的文档以获取详细信息。

由于我没有你的输入数据文件,我无法真正测试它,但这应该产生类似的东西:

{'Ruan': [22], 'hello': [22, 1], 'kurun': [29]}

之后,您的其余代码应该可以正常运行,因为您现在统一了整数列表。无论同一玩家的“得分”数量是多少。

答案 1 :(得分:0)

不确定所有代码在做什么,但使用defaultdict并将所有分数存储在列表中将更容易求和和平均值,defaultdict将添加名称并附加,如果该键不存在或只是如果有的话附加每个分数,它比使用dict.setdefault更有效:

from collections import defaultdict


diction1 = defaultdict(list)
averages_dct = {}
student_average = {}
while choice == 'av': # 
    if schClass == '1': # not sure what this is supposed to do
        schClass = open("scores1.txt", 'r')
    with open("scores1.txt") as f:
        for li in f: # just iterate over the file object
            name, score = li.split(":") # split once and unpack
            # append score cast as int to the list
            diction1[name].append(int(score))
    # now average scores for each using calling sum on lists of ints
    for name,scores in diction1.items():
        student_average = sum(scores) / len(scores)
        averages_dct[name] = student_average

我认为你的下一个循环是找到具有相同平均分数的名称,所以我们再次使用defaultdict使用averages作为键并追加具有相同平均值的名称:

common_dct = defaultdict(list)
# use items to get the key and value
for name, average in averages_dct.items():
    common_dct[averages_dct].append(name)

如果您不想实际使用common_dict,您可以在上一个循环中对名称进行分组,使用分数作为键并附加名称来反转逻辑。

您还可以让statistics模块处理使用以下代码替换代码的平均值:

from statistics import mean
for name,scores in diction1.items():
    student_average = mean(scores)
    averages_dct[name] = student_average