-
Hello, Newbie here. I am trying keep track of our league's trades this year so, ultimately, we can make of fun of the worst trade at the end of the season. To do this, I wrote function to calculate how many starter points each player put up for a team like this: def get_team_starters_value(league, team):
#Initial dict for storing starter:value pairs.
starters = {}
#Iterate through weeks and grab box scores
for k in range(16):
week = k + 1
box_scores = league.box_scores(week)
#Iterate through each box_score until you find team
for i in range(6):
current_box = box_scores[i]
#If team is home_team, add each starter points to dictionary
if current_box.home_team == team:
for player in current_box.home_lineup:
#selects starters only
if player.slot_position != 'BE' and player.slot_position != 'IR':
#create new starter and add points, or add points to if starter already exists.
starters[player.name] = starters.get(player.name, 0) + player.points
#same as above, but if team is away_team
elif current_box.away_team == team:
for player in current_box.away_lineup:
if player.slot_position != 'BE' and player.slot_position != 'IR':
starters[player.name] = starters.get(player.name, 0) + player.points
return starters This function will return a dictionary with each starter's name as the key, and the total starter points as the value. My plan is to do this for each team, and store all that in a dictionary so I can then search for team-specific player values to add to my analysis. The problem is, this function takes over 30 seconds to run (using 2020 data to test), which means in a 12 team league it takes me about 7 minutes to make my master dictionary. This isn't a huge deal, since I'll probably only use it once at the end of the season, but for learning's sake, I'm wondering if anyone has a more efficient way of doing this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
One way to optimize this function would be instead of only saving players for one team, have the main dictionary key be teamId and then have the value be the starters dictionary. This way you would only need to run through this function once and add starters and their points for every team. |
Beta Was this translation helpful? Give feedback.
-
I'm curious - how are you tracking the trade element? It can get tricky, especially in a league with a lot of deal-making where players get picked up, dropped, or traded on to other teams. |
Beta Was this translation helpful? Give feedback.
One way to optimize this function would be instead of only saving players for one team, have the main dictionary key be teamId and then have the value be the starters dictionary.
This way you would only need to run through this function once and add starters and their points for every team.