Python 如何添加以下形式的元组列表中的数字:(';name';,number)

Python 如何添加以下形式的元组列表中的数字:(';name';,number),python,python-3.x,list,sum,tuples,Python,Python 3.x,List,Sum,Tuples,def最高分数(分数,n=5): 每个玩家都会玩游戏若干次,每次都会产生一个元组(名称、分数),表示该玩家在游戏中的得分。假设所有游戏都是这样一个元组列表,将每个玩家的n个最高分数相加,作为该玩家的总分。创建并返回一个列表,其中包含玩家及其总分的元组(名称、总数),按名称升序排序。如果某个玩家玩的次数少于n次,只需将该玩家玩过的任何游戏的分数相加即可 下面是代码,但我想知道是否有更有效的方法来实现这一点,而且使用测试仪似乎也不起作用 def highest_n_scores(scores, n)

def最高分数(分数,n=5):

每个玩家都会玩游戏若干次,每次都会产生一个元组(名称、分数),表示该玩家在游戏中的得分。假设所有游戏都是这样一个元组列表,将每个玩家的n个最高分数相加,作为该玩家的总分。创建并返回一个列表,其中包含玩家及其总分的元组(名称、总数),按名称升序排序。如果某个玩家玩的次数少于n次,只需将该玩家玩过的任何游戏的分数相加即可

下面是代码,但我想知道是否有更有效的方法来实现这一点,而且使用测试仪似乎也不起作用

def highest_n_scores(scores, n):

    bill = sorted([p for (t,p) in scores if t == 'bill'])
    jack = sorted([p for (t,p) in scores if t == 'jack'])
    sheldon = sorted([p for (t,p) in scores if t == 'sheldon'])
    tina = sorted([p for (t,p) in scores if t == 'tina'])
    amy = sorted([p for (t,p) in scores if t == 'amy'])
    bob = sorted([p for (t,p) in scores if t == 'bob'])

    scoreBill = sum(bill[-n:])
    scoreJack = sum(jack[-n:])
    scoreSheldon = sum(sheldon[-n:])
    scoreTina = sum(tina[-n:])
    scoreAmy = sum(amy[-n:])
    scoreBob = sum(bob[-n:])

    return sorted([('bill', scoreBill), ('jack', scoreJack), ('sheldon', scoreSheldon), ('tina', scoreTina), ('amy', scoreAmy), ('bob', scoreBob)])
我得到了正确的结果,但是测试所有值的测试器但是,测试代码的测试器说我的答案是错误的

名单:

[('bill', 10), ('jack', 6), ('sheldon', 3), ('tina', 2), ('amy', 3), ('sheldon', 6), ('tina', 7), ('jack', 2), ('bob', 3), ('bob', 4), ('bill', 3), ('bill', 9), ('sheldon', 5), ('amy', 2), ('jack', 7), ('sheldon', 5), ('sheldon', 7), ('bill', 1), ('bill', 9), ('sheldon', 5), ('bill', 2), ('bill', 6), ('jack', 6), ('bob', 4), ('tina', 5), ('sheldon', 4), ('sheldon', 2), ('amy', 6), ('bob', 7), ('jack', 2), ('bob', 5), ('sheldon', 9), ('jack', 5), ('amy', 9), ('bob', 7), ('tina', 6), ('tina', 2), ('amy', 7), ('jack', 10), ('tina', 4), ('bob', 5), ('jack', 10), ('bob', 7), ('jack', 5), ('amy', 4), ('amy', 8), ('bob', 4), ('bill', 8), ('bob', 6), ('tina', 6), ('amy', 9), ('bill', 4), ('jack', 2), ('amy', 2), ('amy', 4), ('sheldon', 1), ('tina', 3), ('bill', 9), ('tina', 4), ('tina', 9)] when n = 3
列表返回:

[('amy', 26), ('bill', 28), ('bob', 21), ('jack', 27), ('sheldon', 22), ('tina', 22)]

您当前正在创建与名称一样多的变量,这不是非常有效和可伸缩的,相反,您可以考虑使用字典来存储信息

您可以从收集字典中的所有分数开始,以键作为名称,以值作为分数列表。然后遍历字典,对每个名称按降序对分数排序,并找到前n个元素的总和

代码如下所示:

scores = [('bill', 10), ('jack', 6), ('sheldon', 3), ('tina', 2), ('amy', 3), ('sheldon', 6), ('tina', 7), ('jack', 2), ('bob', 3), ('bob', 4), ('bill', 3), ('bill', 9), ('sheldon', 5), ('amy', 2), ('jack', 7), ('sheldon', 5), ('sheldon', 7), ('bill', 1), ('bill', 9), ('sheldon', 5), ('bill', 2), ('bill', 6), ('jack', 6), ('bob', 4), ('tina', 5), ('sheldon', 4), ('sheldon', 2), ('amy', 6), ('bob', 7), ('jack', 2), ('bob', 5), ('sheldon', 9), ('jack', 5), ('amy', 9), ('bob', 7), ('tina', 6), ('tina', 2), ('amy', 7), ('jack', 10), ('tina', 4), ('bob', 5), ('jack', 10), ('bob', 7), ('jack', 5), ('amy', 4), ('amy', 8), ('bob', 4), ('bill', 8), ('bob', 6), ('tina', 6), ('amy', 9), ('bill', 4), ('jack', 2), ('amy', 2), ('amy', 4), ('sheldon', 1), ('tina', 3), ('bill', 9), ('tina', 4), ('tina', 9)]

def highest_n_scores(scores, n = 5):

    scores_dict = {}

    #Make a dictionary of name to list of scores
    for name, score in scores:
        #Set the default value of the dict as an empty list
        scores_dict.setdefault(name, [])
        #Append the score to the name
        scores_dict[name].append(score)

    result_list = []

    #Iterate over the dictionary
    for name, score in scores_dict.items():

        #For total score, sort the list in descending order, and take the sum of first n elements
        total_score = sum(sorted(score, reverse=True)[:n])
        result_list.append((name, total_score))

    return result_list

print(highest_n_scores(scores, 3))
输出将是

[('bill', 28), ('jack', 27), ('sheldon', 22), ('tina', 22), ('amy', 26), ('bob', 21)]

用听写器你可以做到这一点

def highest_n_scores(scores, n):
    result = dict()
    for name , score in scores:
        if name in result.keys():
            result[name].append(score)
        else:
            result[name] = []
            result[name].append(score)

    for key , item in result.items():
        result[key] = sum(result[key][-n:])

    return sorted(result.items(), key=lambda kv: kv[1] , reverse=True)

为什么不使用包含一个人的分数列表的字典呢?为什么需要导入defaultdict以及它是如何使用的?对不起,这可能是个愚蠢的问题,但我是新来的。