Python Forum
Store a set in a dictionary's value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Store a set in a dictionary's value
#8
Ok so I updated my code again and it seems to work so far
def get_dict(team_list):
    winning_dict = {}
    value = 1903
    for index in team_list:
        winning_dict[index] = set()
        if index in winning_dict:
            winning_dict[index].add(value)
        elif index not in winning_dict:
            winning_dict[index] = set(value)
        value += 1

    return winning_dict
Does this look ok to you?

Ok so I guess I do still have some problems with my code. When I try to display the winning years for a team, it is not correct. In fact, each team's winning years I try to display are exactly the same. However, there could be a problem with my code to display the team's winning years. In main I ask the user to enter a team's name and then I display the team's winning years:
#a call to the get_dict function
winning_dict = {}
winning_file = open('WorldSeriesWinners.txt', 'r')
team_list = winning_file.readlines()
winning_file.close()
for index in range(len(team_list)):
     team_list[index] = team_list[index].rstrip('\n')
     winning_dict = get_dict(team_list)

#now to display a team's winning years
key = input("Enter a team's name: ")
for key in winning_dict:
     result = winning_dict.get(key, 'Team not found')
     print(result, end=' ')
But it doesn't matter which team I enter because they all display the same thing:
Output:
{1903} {1994} {1954} {2005} {1908} {1979} {1930} {2007} {1914} {1990} {1948} {2000} {1924} {2006} {1984} {1955} {1957} {1988} {1983} {1986} {1989} {2008} {1985} {1991} {1993} {1995} {2003} {2001} {2002}
First of all, the curly brackes are not supposed to be displayed and these are NOT the correct winning years.

Also, the years are out of order for some reason?
Reply


Messages In This Thread
RE: Store a set in a dictionary's value - by SalsaBeanDip - Oct-05-2020, 10:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Read csv file, parse data, and store in a dictionary markellefultz20 4 4,743 Nov-26-2019, 03:33 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020