2017-03-07 41 views
-2

這裏是輸入CSV該代碼會被讀取隊名蟒蛇遍歷列表中的時間範圍,並制定出平均

team_name half_goals FT_goals shots shots_on_target corners Result 
Arsenal 1 1 16 4 4 L 
Liverpool 1 1 26 11 12 W 
Norwich 0 2 8 2 6 D 
Sunderland 0 0 20 3 6 L 
Swansea 0 1 17 6 7 L 
West Brom 0 0 11 1 4 L 
West Ham 1 2 18 4 4 W 
Chelsea 2 2 22 5 5 W 
Crystal Palace 0 0 5 3 3 L 
Man City 2 4 20 11 8 W 
Chelsea 1 2 15 3 1 W 
Aston Villa 0 0 17 3 8 L 
Everton 0 0 22 8 11 D 
Fulham 0 1 16 7 1 L 

這裏是想閱讀次數團隊其中L碼被提及然後算出平均值

import csv 
from datetime import datetime 
with open('League.csv','r') as csvfile: 
    readCSV = csv.reader(csvfile,delimiter=',') 
    next(readCSV) 
    for row in readCSV: 
     #print(row) 
     lines = [] 
     lines.append(row) 
     #print (lines) 

     teams = set() 

     #print (teams) 
     for line in lines : 
       home_team = line[2] 
       teams.add(home_team) 

       #print (teams) 
       for team in teams: 
         match = [line for line in lines if line[2]==team] 
         #print(match) 

         for i in range(5, len(match)): 
           history = match [i-5:i] 
           average = (history/5) 
           print(average) 

我想要做的是帶來一個平均每5次比賽中提到哪支球隊。

期望輸出

game 1-5 Team one --- average 
game 2-6 Team one --- average 
game 3-7 Team one --- average 

謝謝。

+3

請首先指定輸入/預期輸出... –

+0

謝謝,我已經這樣做了... –

回答

0

您正在使用4嵌套for loops,請將您的代碼簡化爲一個for loop之後。 關於你的問題:

我想要做的是每5次在隊伍中提到一個平均值。

問:...每5次...:請在您的問題說明,也看不到任何球隊5次?
您希望得到的平均值爲FT_goals
更新您的預期產量與來自給定csv的真實數據。