2014-11-05 50 views
0

首先,這是一項家庭作業,但我一直在爲此工作一週,並沒有取得多大進展。我的目標是獲得一個列表清單(每個列表包含一名足球運動員的數據),並根據球員所屬的球隊分開列表。我也想把每個玩家的數據加起來,這樣我就可以將每個玩家的統計數據加在一起,爲每個隊伍列出一個列表。通過迭代在列表中分離列表

下面是我到目前爲止的代碼。我目前遇到的問題是,有些團隊每次都會使用不同的數據多次打印。否則,它似乎工作正常。另外,我們有限制我們不允許使用類的限制。

def TopRushingTeam2010(team_info_2010): #running into trouble calculating the rusher rating for each team, it also prints out the same team multiple times but with different stats. And just not getting the right numbers and order. 
    total_yards = 0 
    total_TD = 0 
    total_rush = 0 
    total_fum = 0 
    #works mostly, but is returning some teams twice, with different stats each time, which 
    #should not be happening. so... yeah maybe fix that? 
    for item in team_info_2010: 
     team = item[0] 
     total_yards = item[2] 
     total_TD = item[3] 
     total_rush = item[1] 
     total_fum = item[4] 
     new_team_info_2010.append([team, total_yards, total_TD, total_rush, total_fum]) 

     for other_item in team_info_2010: 
      if other_item[0] == team: 
       new_team_info_2010.remove([team, total_yards, total_TD, total_rush, total_fum]) 
       total_yards = total_yards + other_item[2] 
       total_TD = total_TD + other_item[3] 
       total_rush = total_rush + other_item[1] 
       total_fum = total_fum + other_item[4] 
       new_team_info_2010.append([team, total_yards, total_TD, total_rush, total_fum]) 

任何幫助或提示,我應該向哪個方向,或者如果我什至朝着正確的方向?

+1

提示:使用字典並遍歷team_info_2010一次。沒有理由將其作爲n平方算法。 – acushner 2014-11-05 15:05:06

回答

1

一個可能的問題是您在遍歷列表時從team_info_2010中刪除。嘗試刪除該行代碼。我沒有看到明確的原因,您爲什麼要從team_info_2010中刪除,並且在迭代對象時修改對象時,行爲通常是未定義的。更具體地說,嘗試刪除以下代碼行:

team_info_2010.remove(item) 
+0

它仍然多次返回同一個團隊 – JodFour 2014-11-05 12:28:41