0

API: https://gist.github.com/nntrn/ee26cb2a0716de0947a0a4e9a157bc1c#v2sportsfootballleaguesnflseasonsyeartypesseasontypeweeksweeknumevents

Chosen API Link: https://sports.core.api.espn.com/v2/sports/football/leagues/nfl/seasons/2022/types/2/weeks/16/events

From this API, I am attempting to extract 12 pieces of data for each team (32 teams), so that I can plug those numbers into a formula and predict the winners of matchups. I created a dictionary to format how I want to store the data here: https://pastebin.com/cK1wJ0ZS (Teams are represented by their "id" in the index) Their ID is assigned in the API.

When I run my data extract file ( https://pastebin.com/LwGus277 ), 60 entries are entered into the categories I have written. This is coming from the loop in my fill_game() method. I do not understand why all 60 are coming through.

for game in range(len(games["items"])):
     self.fill_home_team(games["items"][game]["$ref"])

Each list's length inside of my sub-categories should be equal to the last_games param of __init__, but it is 60 now.

If last_games was 1, then there would only be 1 week's worth of games, and since each team plays once per week, there should be 1 piece of data per category.

If anyone could help that would be greatly appreciated!

I've gone through and followed the instructions line by line as if I was the computer, and still could not understand how these numbers were being looped in.

EDIT: I have tried debugging these lines in PyCharm:

    # Add Turnover Differential
    self.stats[team_id - 1][0]["Turnover Differential"].append(path[10]["stats"][39]["value"])

    # Add RedZone Efficiency
    self.stats[team_id - 1][1]["RedZone Efficiency"].append(path[10]["stats"][12]["value"])

    # Add Third Down Attempts
    self.stats[team_id - 1][2]["Third Down Attempts"].append(path[10]["stats"][29]["value"])

I noticed that instead of putting the team's data in their respective index at [team_id - 1], it is appending the data to EVERY team's list. I cannot figure out why that is happening. Maybe I'm overlooking something?

Raylo
  • 49
  • 6

1 Answers1

0

I did some debugging and can understand why it gave you 60 entries because each week contains the following number of games,

enter image description here

Thus, 16 + 16 + 13 + 15 = 60

The issue is when you initialise the stats object,

change from

self.stats = [n for i in range(32)]

to

self.stats = [[
            {"Turnover Differential": []},  # 0
            {"RedZone Efficiency": []},  # 1
            {"Third Down Attempts": []},  # 2
            {"Third Down Conversions": []},  # 3
            {"Fourth Down Attempts": []},  # 4
            {"Fourth Down Conversions": []},  # 5
            {"Kickoff Yards": []},  # 6
            {"Kickoff Attempts": []},  # 7
            {"Kick Return Yards": []},  # 8
            {"Kick Return Attempts": []},  # 9
            {"Punt Yards": []},  # 10
            {"Punt Attempts": []},  # 11
        ] for i in range(32)]

The previous code gives you 32 references to the same object n (updating one will show the changes in all the items), whereas the new code gives you 32 distinct (independent) objects.

Charles Han
  • 1,920
  • 2
  • 10
  • 19
  • You are 100% on the right track, thank you so much for taking the time to debug! To help you understand what I need as an output, let me explain a bit more. That loop that you deleted loops through each game's link being played that week. Each game has two "competitors" (home team and away team). I am taking each team's "id" from the API and using that to index in my stats array (0-31). For example, the first link in the loop is JAX@NYJ. I want to grab all the data for NYJ since they are home, and then loop to the next link, find the home team, and repeat for 16 games (or len(games)) – Raylo Dec 28 '22 at 15:45
  • Team IDs start at 1, and go to 32 (they skip 31 and 32, and go to 33, and 34., but this is already accounted for). team_id - 1 maps it to the correct index. The issue is that the data that should be at team_id - 1 is being filled out for EVERY index (0-32) and repeats for each game. It will not only fill out all data for that team's index that I provided. That is my issue. – Raylo Dec 28 '22 at 20:16
  • @Raylo, I have updated my answer. Your investigation also helped to identify the issue. High-five to both of us! – Charles Han Dec 28 '22 at 20:56