如何添加以以下形式给出的元组列表形式给出的数字:(“名称”,数字)

时间:2019-06-08 22:11:55

标签: python python-3.x list sum tuples

def maximum_n_scores(得分,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)]

2 个答案:

答案 0 :(得分:1)

您当前正在创建与名称一样多的变量,这不是非常有效且可扩展,因此您可以考虑使用字典来存储信息

您可以从收集字典中的所有乐谱开始,其中键为名称,值为乐谱列表。然后,您遍历字典,对于每个名称,将分数按降序排序,并找到前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)]

答案 1 :(得分:0)

使用字典可以做到这一点。

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)