EPLgames2018/19 CSV我对python还是个新手。
我正在阅读一个CSV文件,其中包含了2018/19赛季每一场英语联赛的统计数据。
我已经创建了一个所有球队的列表。然后我试图轮流拿每支球队,并循环通过所有的比赛来计算每支球队本赛季的总积分。
这似乎对一线队很有效。它需要曼联,我为他们得到正确的分数。我遇到的问题是进入名单中的下一支球队,然后和他们一起循环积分代码。
import csv
with open('EPL1819.csv') as file:
eplgames = csv.DictReader(file)
teampoints = list()
eplteams = list()
teamcount = 0
count = 0
# Outer loop going through teams one at a time
for i in range(20):
points = 0
# Inner loop going through each match
for x in eplgames:
# Populates the eplteams list
if x['HomeTeam'] not in eplteams:
eplteams.append(x['HomeTeam'])
teamcount += 1
#print(eplteams[i])
# Works out the match result
if x['FTHG'] > x['FTAG']:
match_result = x['HomeTeam']
elif x['FTHG'] < x['FTAG']:
match_result = x['AwayTeam']
else:
match_result = "Draw"
if eplteams[i] == match_result:
points += 3
if eplteams[i] == x['HomeTeam']:
if match_result == "Draw":
points += 1
if eplteams[i] == x['AwayTeam']:
if match_result == "Draw":
points += 1
# Populates the teampoints list
teampoints.append(points)
print(eplteams[i])
print("Points:", points)
print("Points List:", teampoints[i])
2条答案
按热度按时间ecfsfe2w1#
您正在根据某些条件将小组添加到eplteams:
然后在条件中使用eplteam元素:
看起来不对。eplteams中的元素i此时可能不存在。
g0czyy6m2#
你需要在循环eplgames之前填充,否则它可能找不到正在比赛的球队:
循环前填充: