1483. 最高平均分

1483. 最高平均分

中文English

给出一组学生的名字以及他们的成绩,求最高的平均分(一位学生可能有多门课成绩)

样例

Example 1:

Input:names = ["bob","ted","ted"]
grades=[88,100,20]
Output:88

Example 2:

Input: names = ["john","xisa","xisa","liajd","alice","john","xisa","mark","ted","xlisa"]
grades = [95,83,33,50,78,91,80,67,85,87]
Output:93 
输入测试数据 (每行一个参数)如何理解测试数据?

 

class Solution:
    """
    @param names: the name
    @param grades: the grade
    @return: the maximum average score
    """
    '''
    大致思路:
    1.初始化字典,dic = {},例{'alex':[]...},然后如果是同名的话,则将值append到value里面,最终循环dic,取出最大平均值即可。
    '''
    def maximumAverageScore(self, names, grades):
        dic = {}
        for i in range(len(names)):
            if names[i] not in dic:
                dic[names[i]] = [grades[i]]
            else:
                dic[names[i]].append(grades[i])
        
        max_num = 0
        for k,v in dic.items():
            if sum(v)/len(v) > max_num:
                max_num = sum(v)/len(v)
        return max_num

 

posted @ 2020-04-25 15:48  风不再来  阅读(113)  评论(0编辑  收藏  举报