{'Stephen Curry': [(22.6, 5.1, 3.4)], 'Draymond Green': [(16.5, 10.3, 6.3)], 'Lebron James': [(29.7, 11.3, 8.9)]})
在这种情况下,每个值对应一个catergory格式为(pts,reb,assists)我正在尝试检索每个玩家之间的pts,reb和ass的最大值并写入文件。我无法获得与正确密钥对应的最大值。
def writeMVP(myfile,statAverages):
myfile = open(myfile,"w")
myfile.write("NBA Finals Stats")
index = 0
for item in statAverages:
maxstat= max([avg[0][index] for avg in statAverages.values()])
index = index + 1
答案 0 :(得分:0)
我会找到与最大值对应的键,如下所示:
In [13]: max_pts_player = max(stats, key=lambda x:stats[x][0][0])
In [14]: max_pts_player
Out[14]: 'Lebron James'
In [15]: max_reb_player = max(stats, key=lambda x:stats[x][0][1])
In [16]: max_reb_player
Out[16]: 'Lebron James'
In [17]: max_assits_player = max(stats, key=lambda x:stats[x][0][2])
In [18]: max_assits_player
Out[18]: 'Lebron James'
然后你可以使用与stat对应的玩家名称和元组索引来检索max stat:
In [19]: max_pts = stats[max_pts_player][0][0]
In [20]: max_pts
Out[20]: 29.7
但如果你想从stat开始,
In [5]: max_pts
Out[5]: 29.7
In [6]: list(filter(lambda k:stats[k][0][0] == max_pts, stats))Out[6]: ['Lebron James']
如果多个玩家拥有最大统计数据,这种方式可能会更好。
答案 1 :(得分:0)
好的,据我所知,这就是我提出的:
d = {'Stephen Curry': [(23.6, 5.1, 3.4)], 'Draymond Green': [(16.5, 10.3, 6.3)], 'Lebron James': [(29.7, 11.3, 8.9)]}
lst_tuples = sorted(d.items())
max_pts = max(lst_tuples, key = lambda x: x[1][0][0])
max_rebs = max(lst_tuples, key = lambda x: x[1][0][1])
max_assists = max(lst_tuples, key = lambda x: x[1][0][2])
print_stat_1 = "Max points player: {0}, with a score of {1}.\n" \
.format(max_pts[0], max_pts[1][0][0])
print_stat_2 = "Max rebs player: {0}, with a score of {1}.\n" \
.format(max_rebs[0], max_rebs[1][0][1])
print_stat_3 = "Max assists player: {0}, with a score of {1}.\n" \
.format(max_assists[0], max_assists[1][0][2])
with open("best_player_stats.txt", "w") as text_file:
text_file.write("NBA Finals Stats\n")
text_file.write("----------------\n")
text_file.write("%s\n%s\n%s\n" % (print_stat_1, print_stat_2, print_stat_3))
with open("best_player_stats.txt","r") as file:
print(file.read())
打印出来:
NBA Finals Stats
----------------
Max points player: Lebron James, with a score of 29.7.
Max rebs player: Lebron James, with a score of 11.3.
Max assists player: Lebron James, with a score of 8.9.
如果我没有做对,请在下面的评论中告诉我。
答案 2 :(得分:0)
data = {'Stephen Curry': [(22.6, 5.1, 3.4)],
'Draymond Green': [(16.5, 10.3, 6.3)],
'Lebron James': [(29.7, 11.3, 8.9)]}
max_pts_player = list(reversed(sorted(data, key=lambda x: data[x][0][0])))[0]
max_reb_player = list(reversed(sorted(data, key=lambda x: data[x][0][1])))[0]
max_assits_player = list(reversed(sorted(data, key=lambda x: data[x][0][2])))[0]
with open("nba_stats.txt", "w") as f:
f.write("NBA Finals Stats\n")
f.write("Maximum points player is " + max_pts_player + ", with a points of " + str(data[max_pts_player][0][0]) + ".\n")
f.write("Maximum rebs player is" + max_reb_player + ", with a points of " + str(data[max_reb_player][0][1]) + ".\n")
f.write("Maximum assits player is" + max_assits_player + ", with a points of " + str(data[max_reb_player][0][2]) + ".\n")