Posts: 4,220
Threads: 97
Joined: Sep 2016
You could load your dictionary in a loop. If your team_stats list is just a list of sub-lists of the form [team_name, strength, form, injuries, motivation, city], it would look like this
dict_teams = {}
for team in team_stats:
dict_teams[team[0]] = Teams(*team) The *team syntax turns the list in a series of parameters to the Team instance. The above loop can be turned into a dictionary comprehension:
dict_teams = {team[0]: Teams(*team) for team in team_stats}
Posts: 26
Threads: 4
Joined: Nov 2018
Wow, that worked perfect, thank you!
I just added an extra for-loop to iterate over the different team columns;
dict_teams = {}
for team in team_stats:
for i in range(columns):
dict_teams[team[i]] = Teams(*team) Now I'll try to build the add- functions. So most likely I'll soon be here again asking for more help... :) thanks again!
Posts: 26
Threads: 4
Joined: Nov 2018
I’m trying to add the home- and away results for each team i create. My idea was to add a home_result list and an away_result list to the class attributes like below. Then i want to run the add function every time a new class instance is created.
This doesn't work because last_row, results and the increments i and j are not defined. I then tried to put the for loop in a function to witch i could send the arguments, but that didn't work either… Any good ideas?
class Teams:
def __init__(self, legaue ="", name = "", strength = 0, form = 0, injuries = 0, motivation = 0, city="", home_results = [], away_results = []):
self.legaue = legaue
self.name = name
self.strength = strength
self.form = form
self.injuries = injuries
self.motivation = motivation
self.city = city
self.home_results = home_results
self.away_results = away_results
for row in range(last_row):
if self.name == results[row][2]:
self.home_results[row] = (results[row][4])
i += 1
if self.name == results[row][3]:
self.away_results[j] = (results[row][5])
j += 1
Posts: 4,220
Threads: 97
Joined: Sep 2016
Don't loop through the results in the team initialization. If you do it that way, you'll have to go through all the results once for each team. Initialize all the teams based on their names and the stats you created for them. Then, once you have all of the teams in team_dict, read through the results:
with open('path/to/results.txt') as results_file:
for row in results_file:
team_dict[row[4]].home_results.append(row)
team_dict[row[5]].away_results.append(row) I would even suggest add_away and add_home methods like I discussed earlier, so that all the results could be in the same format (have the team in question's score in the same place, that sort of thing).
Posts: 26
Threads: 4
Joined: Nov 2018
Thanks again, your solution is so much clever, saves a lot of iterations! However, i don't get the results i want.
My code looks like this: (results = result_file in your example, row[2] = home team name)
for row in results:
dict_teams[row[2]].home_results.append(row)
for i in range(5):
print(dict_teams["Bournemouth"].home_results[i]) I just put the line print(dict_teams["Bournemouth"].home_results[i]) in to see how it worked. I was expected the 5 latest home scores for Bournemouth, but instead i end up with:
['E0', '10/08/18', 'Man United', 'Leicester', '2', ’1']
['E0', '11/08/18', 'Bournemouth', 'Cardiff', '2', '0']
['E0', '11/08/18', 'Fulham', 'Crystal Palace', '0', '2']
['E0', '11/08/18', 'Huddersfield', 'Chelsea', '0', '3']
['E0', '11/08/18', 'Newcastle', 'Tottenham', '1', '2']
So regardless what team name i put in, i just get the same results.
Btw, this code above i just placed in teh main function. I don't really understand how to use or the advantages of the add_methods?
Sorry for beeing sucha newbie here, really want to learn this and i think i'm quite near now.
Posts: 4,220
Threads: 97
Joined: Sep 2016
Look back at post #13, you have home_results = [] in the initialization. That's a problem. Lists are mutable, the are just passed as a reference to the list, not a copy of the list. Also, default parameters are stored in the function or method, and are not resolved every time the method is called. So every team's home_results is referencing the same list. That's why they have the same values in them.
I think the easiest solution here would be to remove that parameter from the parameter list. Instead just have home_results = [] in the body of the __init__ method. If it's in the body of the code, it will be resolved fresh each time the code is run, and you'll get five different lists.
Posts: 26
Threads: 4
Joined: Nov 2018
Wow, that would have taken me a year to figure out on my own. Thanks! :)
Posts: 4,220
Threads: 97
Joined: Sep 2016
I said to take out the parameter, because I didn't think you would ever be passing home_results to a new Team class. If you might be passing a list to a class, but you want it to default to an empty list, do this:
class Spam(object):
def __init__(self, name, some_list = None):
self.name = name
if some_list is None:
self.some_list = []
else:
self.some_list = some_list
Posts: 26
Threads: 4
Joined: Nov 2018
I have problem accessing the class from another file. I think it has something to do with the dictionary i use to store the classes? I import the class and the function, but the error message says the dictionary is not defined?
Posts: 4,220
Threads: 97
Joined: Sep 2016
What's your current code look like?
|