I've got another question. You guys have been super helpful.
So I created a dictionary between NFL team names and their codes. The dictionary looks like this:
team_code = []
franchise_list = []
for row in NFL_teams:
franchise = str(row["team_name"])
team_id = str(row["team_id"])
team_code.append(team_id)
franchise_list.append(franchise)
nfl_dict = {}
for a,b in zip(team_code,franchise_list):
nfl_dict.setdefault(a,[]).append(b)
This dictionary has multiple team names (values) attached to 3 letter team codes (key). For example:
organization = input("Pick a team: ") <- LAR as input
print(nfl_dict[organization]) <- ['Los Angeles Rams', 'St. Louis Rams', 'Los Angeles Rams'] as output
Later, I need to input a team exactly as it shows in the dataset in order to get back its record. A portion of that code looks like this:
season_year = input("Pick a year from 1966 onwards: ")
team = input("Pick a team: ")
for row in NFL_stats:
if row["schedule_season"] == season_year:
home = row["team_home"],row["score_home"]
away = row["team_away"],row["score_away"]
if team == home[0]:
my_team = home
other_team = away
elif team == away[0]:
my_team = away
other_team = home
else:
continue
However, a team's name is not the same from year to year, as shown above with the LA Rams. And as it stands, if I enter a year where the team was in St. Louis as opposed to Los Angeles (for example 1999), I won't get a record back since the data states "St. Louis Rams".
I want to be able to enter the dictionary key (the three-letter code) instead of the team name. How would I be able to do so?
Thanks in advance.
As Samwise commented, there is a lot missing from your post, such that a full understanding is difficult. But the question you ask seems to just be, how to find something in a Python dictionary from its key. If that's the case, here is the simple answer, from the Udemy Python course I took:
That code returns 'value2'.
2/25/2022 update: Based on your comment, there are several ways to avoid the error you are getting. I thing you need something like along the following lines. I calculated the number of items with "len" just to show you, but I didn't even need that:
The output of that code is:
Another approach to error messages, like you are getting, is to use a "try / except" block of code. But that is probably not as appropriate here.
If you are interested, I just learned all this stuff in a online course by Udemy. I really liked it: https://www.udemy.com/course/python-from-zero-to-hero/
Eliot