我们在两级词典中代表一系列匹配的击球手,如下所示:
{'match1':{'player1':57,'player2':38},'match2':{'player3':9,'player1':42},'match3':{'player2':41 ,'player4':63,'player3':91}
每个比赛由一个字符串标识,每个玩家也是如此。分数都是整数。与比赛相关联的名称不固定(这里是'match1','match2','match3'),也不是球员的名字。玩家不需要在所有比赛中记录得分
定义一个Python函数“orangecap(d)”,它读取此表单的字典d并标识总得分最高的玩家。你的函数应该返回一对(playername,topscore),其中playername是一个字符串,得分最高的玩家的名字,topscore是一个整数,即玩家名称的总得分。
输入将是最高总分从来没有任何联系。
例如:
orangecap({'match1':{'player1':57,'player2':38},'match2':{'player3':9,'player1':42},'match3':{'player2' :41,'player4':63,'player3':91}}) ('player3',100)
orangecap({'test1':{'Ashwin':84,'Kohli':120},'test2':{'ashwin':59,'Pujara':42}}) ('Kohli',120)
这是我的代码:
def orangecap(d):
s=[]
t=[]
for i in sorted(d.keys()):
for j in sorted(d[i].keys()):
flag=0
for k in range(len(s)):
if(s[k]==j):
t[k:k]+=[d[i][j]]
flag=1
break
if(flag==0):
s.append(j)
t.append(d[i][j])
m=max(t)
for i in range(len(t)):
if t[i]==m:
return (s[i],m)
答案 0 :(得分:0)
你没有在那里得到一个问题,将来尝试具体说明你的问题或尝试https://codereview.stackexchange.com/一般指针等。
首先要在python中注意,因为在命名变量时,所有编程语言都尽可能地表达,这将使每个人(通常是你自己)更容易理解正在发生的事情(和错误的事情)。
所以在这个问题中,我们试图找出每个球员的总得分。换句话说,我们将每个匹配的字典组合成一个字典,如果我们有一个重复的密钥则添加。所以
def orangecap(d):
# Make a new dict to count the totals
totals = {}
# Loop over every players score in every match.
for match in d.values():
for player in match:
# Check if player is in totals dictionary. If not add them.
if player not in totals:
totals[player] = 0
# Add players score in this match to there total score.
totals[player] += match[player]
#initialise highest_total as we will use it later for comparison.
highest_total = -1
#loop through totals to find highest score
for player in totals:
# if players score is greater than current highest, set highest_player
# to be current player, and set the highest total to players total
if totals[player] > highest_total:
highest_player = player
highest_total = totals[player]
# return the highest player and highest total
return highest_player, highest_total