2016-06-29 45 views
1
{'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在這種情況下,格式爲(分,籃板,助攻),我試圖找回最大值關鍵每個玩家之間的點數,回扣和助攻數,並寫入文件。我無法獲得與正確密鑰對應的最大值。如何檢索與具有價值的元組(蟒蛇)一定值

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

乍一看,它可能是你打算在循環體中說「item」而不是「statAverages」? –

+0

你可以給我們這個代碼的輸出嗎? – RoadRunner

+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' 

然後你可以檢索使用對應於統計玩家的名字和元組指數的最大靜:

In [19]: max_pts = stats[max_pts_player][0][0] 

In [20]: max_pts 
Out[20]: 29.7 

但是,如果你想與統計開始了,

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'] 

這種方式可能會更好,如果不止一名玩家可以擁有最大數量。

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. 

如果我沒有做正確的事,請讓我知道在下面的評論。

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")