Posts: 26
Threads: 4
Joined: Nov 2018
Hi there,
I'm new to Python, have previously done a little C but nothing to deep. I want to build a program to sort out soccer results from a CSV file and thought it would be good to get som pointers from more experienced coders to have a proper set-up.
So far i have read the csv-file and divided it to a 2D array. In total it's around 1700 rows, the first ones looks like this:
[['Country', 'League', 'Season', 'Date', 'Time', 'Home', 'Away', 'HG', 'AG', 'Res', 'PH', 'PD', 'PA', 'MaxH', 'MaxD', 'MaxA', 'AvgH', 'AvgD', 'AvgA']. ['Sweden', 'Allsvenskan', '2012', '12-03-31', '15:00', 'Elfsborg', 'Djurgarden', '2', '1', 'H', '1,71', '3,98', '5,44', '1,73', '3,98', '5,5', '1,66', '3,68', '5'], ['Sweden', 'Allsvenskan', '2012', '12-03-31', '15:00', 'GAIS', 'Hacken', '0', '0', 'D', '2,21', '3,57', '3,48', '2,5', '3,57', '3,5', '2,25', '3,24', '3,11'] --- and so on....
I want to make a program that lets me choose a team and a number of games. Then i get the stats for that number of games for that team. For example "Team A" + "4 games", gives me the data for the latest 4 games for "Team A". Later in the program i want to work with the chosen stats and calculate form for chosen team and predictions for upcoming games.
So.. sorry for long introduction... my question is, what's the best way to set this up? I've been thinking of using dictionary for each game, or is it better to use classes for every team? Or maybe I don't need any of that. Perhaps it's easier to just sort the list after teams and dates of games and then just chose the number of games to look at?
Hope to get some good input here before i just spending hours on a dead-end solution.
Thanks in advance
Posts: 12,026
Threads: 485
Joined: Sep 2016
Posts: 4,220
Threads: 97
Joined: Sep 2016
I would probably do a Team class, with a list of results as an attribute. Put the teams in a dict keyed by their name. Then it's simple to get the info you need. But I would look over your requirements carefully. There may be something else you want to do that would work better with a different structure.
Posts: 26
Threads: 4
Joined: Nov 2018
Nov-11-2018, 06:36 PM
(This post was last modified: Nov-11-2018, 06:51 PM by tobbs.)
Here is the code so far. Haven't done much yet, want to have the structure mapped out before i spend hours coding something that doesn't work the way i want :)
import os
import string
# Open and read csv-file, save list as H_team (Home)
with open("allsvenskan.csv") as csv_file:
H_teams = csv_file.readlines()
# Split to seperate words, make H_team to 2D array. Make copy for A_team (Away)
H_teams = [x.strip().split(";") for x in H_teams]
A_teams = H_teams
# Count number of rows
sista_rad = len(H_teams)
# Sort in alphabetic order by home team and away team (they are already sorted by date from input file)
H_teams.sort(key=lambda x: x[5])
A_teams.sort(key=lambda x: x[6])
# Make slecetion to chose team and number of matches to check
team_select = input("Choose team")
num_games_select = input("Number of games")
@ ichabod801
I think there are about 20 different teams in the list, each with between 10 and 100 home games and equally as many away games each. i guess this is too much to handle. However, i only need around 10 games per team (last 5 home and 5 away) to continue with calculations. Do you still recommend Team class for this?
Posts: 4,220
Threads: 97
Joined: Sep 2016
Here's the thing: at some point you are going to have to filter out the games for each team. You can either do this when you load the data, or you can do it when you get the user's query. It seems simpler to me to do it when you load the data, but it's your choice. If you do it when you load the data, that suggests a class. It could however just be a dictionary of team names to lists of game rows.
Note that your code sorts H_teams by the away team name. This is because H_teams and A_teams are pointing to the same list. You want A_teams = H_teams[:] for line 11, to make a shallow copy of H_teams that can be sorted independently.
Posts: 26
Threads: 4
Joined: Nov 2018
Thanks for good input! I think i only need maximum 5-6 games for each team so i guess i can save some memory/time just loading that data after user query?
Thanks with the copying hint, i didn't think about that.
Posts: 4,220
Threads: 97
Joined: Sep 2016
I don't think that's going to be a concern in this case. That would only come up with a ton of teams/games.
Posts: 26
Threads: 4
Joined: Nov 2018
I’ve been working with this quite a bit, but, to be honest i’m lost… So far I’ve managed to sort the list the way i want and extract data from it, for example the latest 5 games for chosen team. These result i use to calculate a form index for each team. I then want to add additional general data for each team. The problem is i cant figure out where/ how to store the data. Can’t figure out if teams should be class, dictionary or just a list…
in the end, i want a Team class (or rather dictionary??) that store this data for each team;
Strength = value given by me
Form = Form calculated by result from latest 5 games
Injuries = value given by me
Motivation = value given by me
City = value given by me
Probably more data that i just haven’t thought about know yet…
I then want to extract this data for chosen two teams to compare and make predictions.
———————————————————————————————————————————————————————
My plan:
1. Define a Team class that i can access from separate functions. Set up every team in the league as Team class object.
2. Make function (separate file) to input value for strength, injures, motivation, city
3. Make function(separate file) to read csv-file with results, use results to calculate form for each team
4. Make function (separate file) to compare two team objects
5. Make main file (separate file) to run functions above
6. Develop further and add more functions/ stats to program… :)
Would this be a good set-up?
Where do store the Team Class and all the team objects? In main or separate file?
Could the Team class look like this?
class Team:
# Attributes for teams
def __init__(self, name = "", strength = 0, form = 0, injuries = 0, motivation = 0, city=""):
self.name = name
self.strength = strength
self.form = form
self.injuries = injuries
self.motivation = motivation
self.city = city ———————————————————————————————————————————————————————
If someone has the patience to help me out here it would be great. I know i'm asking basic things, but i think learning by doing is a good way to make progress :)
Posts: 4,220
Threads: 97
Joined: Sep 2016
The Team class could be like that, certainly. Note that the Team class could also have methods. For example, the function comparing two teams could easily be a method of the class, so you could do something like redskins.compare(cowboys) . I would also have add_home and add_away methods. Each one would just take a row from the csv file, and add a game to the team object, taking into account whether that team was the home or away team. Then you could have code like this for reading the csv_file:
for line in game_file:
fields = line.split(',')
teams[fields[5].strip()].add_home(line)
teams[fields[6].strip()].add_away(line) Where teams is a dictionary with team names as keys and team objects as values.
Posts: 26
Threads: 4
Joined: Nov 2018
Thanks for the help so far, good idea with the add functions!
I made a dictionary of teams and added the Team class for every team.
class Teams:
# Attributes for teams
def __init__(self, name = "", strength = 0, form = 0, injuries = 0, motivation = 0, city=""):
self.name = name
self.strength = strength
self.form = form
self.injuries = injuries
self.motivation = motivation
self.city = city
dict_teams = {name: Teams(name) for name in team_list} I also have a list (team_stats) with values strength, form, injuries, motivation and city for every team. I just cant figure out how to add it to the objects i created?
|