Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Jupyter notebook coursenotes/Topic2/Lab Exercise 2.ipynb

22 views
Kernel: Anaconda (Python 3)
import numpy as np data = open('PremierLeague2011.txt') teams = ('Aston Villa','Arsenal ','Blackburn','Bolton ','Chelsea ','Everton ','Fulham ','Liverpool','Man City','Man United','Newcastle','Norwich ','QPR ','Stoke ','Sunderland','Swansea ','Tottenham','West Brom','Wigan ','Wolves ') points = [0] * 20 matchesplayed = [0] * 20 goalsfor = [0] * 20 goalsagainst = [0] * 20 matcheswon = [0] * 20 matchesdrawn = [0] * 20 matcheslost = [0] * 20 Titles = ('Team ','PL', 'W','D','L','GF','GA','POINTS') class PremierLeagueTeam(object): def __init__(self, Date, code_H, code_A, Scored, Conceded, Result): self.date = Date self.codeH = code_H self.codeA = code_A self.scored = Scored self.conceded = Conceded self.result = Result def tot_points(self): if (self.result == 'H'): points[int(self.codeH)] += 3 if (self.result == 'A'): points[int(self.codeA)] += 3 if (self.result == 'D'): points[int(self.codeA)] += 1 points[int(self.codeH)] += 1 def matches(self): matchesplayed[int(self.codeH)] += 1 matchesplayed[int(self.codeA)] += 1 def goals(self): goalsfor[int(self.codeH)] += self.scored goalsagainst[int(self.codeH)] += self.conceded goalsfor[int(self.codeA)] += self.conceded goalsagainst[int(self.codeA)] += self.scored def WDL(self): if (self.result == 'H'): matcheswon[int(self.codeH)] += 1 matcheslost[int(self.codeA)] += 1 if (self.result == 'A'): matcheswon[int(self.codeA)] += 1 matcheslost[int(self.codeH)] += 1 if (self.result == 'D'): matchesdrawn[int(self.codeH)] += 1 matchesdrawn[int(self.codeA)] += 1 for line in data: F = [] line = line.split() for i in line: if i == line[0] or i == line[5]: i = i F.append(i) else: i = float(i) F.append(i) A = PremierLeagueTeam(F[0],F[1],F[2],F[3],F[4],F[5]) PremierLeagueTeam.tot_points(A) PremierLeagueTeam.matches(A) PremierLeagueTeam.goals(A) PremierLeagueTeam.WDL(A) zipped = zip( teams, matchesplayed, matcheswon, matchesdrawn, matcheslost, goalsfor, goalsagainst, points) sortedlist = sorted(zipped, key = lambda x: x[7], reverse = True) print (*Titles, sep='\t') for a in sortedlist: print(*a, sep='\t')
Team PL W D L GF GA POINTS Man City 15 12 2 1 49.0 15.0 38 Man United 15 11 3 1 35.0 14.0 36 Chelsea 15 10 1 4 33.0 18.0 31 Tottenham 14 10 1 3 30.0 18.0 31 Arsenal 15 9 2 4 31.0 23.0 29 Liverpool 15 7 5 3 18.0 13.0 26 Newcastle 15 7 5 3 21.0 19.0 26 Stoke 15 6 3 6 16.0 24.0 21 Aston Villa 15 4 7 4 18.0 19.0 19 Norwich 15 5 4 6 24.0 28.0 19 Swansea 15 4 5 6 16.0 20.0 17 Everton 14 5 1 8 15.0 18.0 16 QPR 15 4 4 7 15.0 26.0 16 Fulham 15 3 6 6 16.0 18.0 15 West Brom 15 4 3 8 14.0 23.0 15 Sunderland 15 3 5 7 18.0 18.0 14 Wolves 15 4 2 9 16.0 28.0 14 Wigan 15 3 3 9 14.0 29.0 12 Blackburn 15 2 4 9 22.0 34.0 10 Bolton 15 3 0 12 20.0 36.0 9