gpt4 book ai didi

python - 如何添加以 : ('name' , 形式给出的元组列表给出的数字(数字)

转载 作者:行者123 更新时间:2023-12-01 07:44:53 25 4
gpt4 key购买 nike

def Highest_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)]

最佳答案

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

您可以首先收集字典中的所有分数,将键作为名称,将值作为分数列表。然后,您迭代字典,对于每个名称,按降序对分数进行排序,并找到前 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)]

关于python - 如何添加以 : ('name' , 形式给出的元组列表给出的数字(数字),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56510483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com