#πŸ”’ Dealing with Lists that are inconsistent in length and contents.

33 messages Β· Page 1 of 1 (latest)

toxic thunder
#

I'm using the CFBD API for College Football Stats. The "Stats" list is not a consistent order and the stats listed are not consistnet. I would like to print the same stats into the same column in excel as I iterate through the games, but again, the stats are not always in the same order when I output the API contents.

Example of lists:

loud smeltBOT
#

@toxic thunder

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

rough locust
#

Before you store them in the columns

toxic thunder
#

i've done that, the problem is that some stats are not in every game output

#

so sorting would work if the outputs were always the same length, but they're not

rough locust
#

These results are being returned in json right?

toxic thunder
#

no it's not json

#

its a dictionary though

wispy prairie
#

not convinced that it's inconsistent, sounds like your parsing is just lacking

rough locust
#

Can you send an example of a real result, and also send your code?

wispy prairie
#

your screenshots show nothing btw, they're cropped into uselessness

toxic thunder
#
sorted_data = sorted(stats, key=lambda x: x.category)
#

thats what i use to sort, and then i went to write everything to excel, began to see the lists were of inconsistent length, and got stuck

#

here are 2 actual outputs:

#

there was no parsing done prior to these being written to excel

rough locust
#

!paste Can we see your whole code?

loud smeltBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

toxic thunder
#
api_instance = cfbd.GamesApi(cfbd.ApiClient(configuration))
game_id = 401520155  # int | Game id filter (optional)
year = 2023 # int | Year/season filter for games (optional)
#week =  # int | Week filter (optional)
season_type = 'regular' # str | Season type filter (regular or postseason) (optional) (default to regular)
team = 'illinois'# str | Team (optional)
#home = 'home_example' # str | Home team filter (optional)
#away = 'away_example' # str | Away team filter (optional)
#conference = 'conference_example' # str | Conference abbreviation filter (optional)"""
cfbd.GamesApi(cfbd.ApiClient(configuration))

api_instance2.get_team_game_stats(team=team, year=year)
stats = api_response2[0].teams[0].stats


sorted_data = sorted(stats, key=lambda x: x.category)
wispy prairie
#

I don't see much to respond to really. presumably what you're missing is code that knows which fields it cares about and tolerates them being missing, instead of just sorting the raw data

toxic thunder
#

sorry, not sure i'm following. That is my question, how do I write this to excel, consistently, so that the same stats get written into the same columns consistenly

#

right now, that's hard to do giving some stats are not in every output

wispy prairie
#

you might create a list of things you care about, then loop through that list and extract each such thing from the data

#

that way you always get it in the order of the list you created

toxic thunder
#

like create a list of the categories i care about?

#

also, again, not every stat is listed for every game, so i think that will not work

wispy prairie
#

you'd find that out when looking it up

#

the missing data is going to be missing, there isn't a way of doing this that changes that

toxic thunder
#

so your suggestion is create a list of the stats I want and order them how i want? I will have to google that or look on substack, so just want to verify

#

thanks anyway

#

!close

loud smeltBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.