在Python上用此代码创建一个字典,这是另一个排序字典,谢谢

时间:2020-04-20 02:19:41

标签: python windows dictionary

但是,我已经完成了所有工作。我无法弄清楚如何用我创建的字典制作排序字典。该词典应具有赢得最多比赛的队伍,排名第一,依此类推。

这是我创建的第一本词典 img of dictionary

我尝试过 sorted_wins = sorted(matches_won[team].items(), key=lambda pair: pair[1], reverse=True)

但是我一直遇到:AttributeError:'int'对象没有属性'items'

fp = open("C:/Users/Owner/Documents/WorldSeries.txt", "r")

matches = {}

matches_won = {}

year = 1903

for team in fp.readlines():

    team = team.strip()

    matches[year] = team

    if team not in matches_won:

        matches_won[team] = 0

    matches_won[team]+=1

    year += 1

fp.close()
sorted_wins = sorted(matches_won[team].items(), key=lambda pair: pair[1], reverse=True)
for team in matches_won:

    print(team,":",matches_won[team])

again = "Y"

while again=="Y":

    year = int(input("\n Enter a year in the range of 1903 through 2019: "))

    if(year>=1903 and year<=2017):

        print(matches[year], "team won match in",year)
        print(matches_won[team])

else:

    print("Invalid year")

again = input("\nWant to continue playing?(Y - yes, N - no): ").upper()

尝试使用现有方法,我仍然只有这本词典

Dictionary

这是字典:

matches_won = {'Boston Americans' : 1,
'World Series Not Played in 1904' : 1,
'New York Giants' : 5,
'Chicago White Sox' : 3,
'Chicago Cubs' : 3,
'Pittsburgh Pirates' : 5,
'Philadelphia Athletics' : 5,
'Boston Red Sox' : 8,
'Boston Braves' : 1,
'Cincinnati Reds' : 5,
'Cleveland Indians' : 2,
'New York Yankees' : 27,
'Washington Senators' : 1,
'St. Louis Cardinals' : 11,
'Detroit Tigers' : 4,
'Brooklyn Dodgers' : 1,
'Milwaukee Braves' : 1,
'Los Angeles Dodgers' : 5,
'Baltimore Orioles' : 3,
'New York Mets' : 2,
'Oakland Athletics' : 4,
'Philadelphia Phillies' : 2,
'Kansas City Royals' : 2,
'Minnesota Twins' : 2,
'Toronto Blue Jays' : 2,
'World Series Not Played in 1994' : 1,
'Atlanta Braves' : 1,
'Florida Marlins' : 2,
'Arizona Diamondbacks' : 1,
'Anaheim Angels' : 1,
'San Francisco Giants' : 3,
'Houston Astros' : 1,
'Washington Nationals' : 1}

1 个答案:

答案 0 :(得分:1)

让我们假设:

 matches_won = {'Boston Americans' : 1,
'World Series Not Played in 1904' : 1,
'New York Giants' : 5,
'Chicago White Sox' : 3,
'Chicago Cubs' : 3,
'Pittsburgh Pirates' : 5,
'Philadelphia Athletics' : 5,
'Boston Red Sox' : 8,
'Boston Braves' : 1,
'Cincinnati Reds' : 5,
'Cleveland Indians' : 2,
'New York Yankees' : 27,
'Washington Senators' : 1,
'St. Louis Cardinals' : 11,
'Detroit Tigers' : 4,
'Brooklyn Dodgers' : 1,
'Milwaukee Braves' : 1,
'Los Angeles Dodgers' : 5,
'Baltimore Orioles' : 3,
'New York Mets' : 2,
'Oakland Athletics' : 4,
'Philadelphia Phillies' : 2,
'Kansas City Royals' : 2,
'Minnesota Twins' : 2,
'Toronto Blue Jays' : 2,
'World Series Not Played in 1994' : 1,
'Atlanta Braves' : 1,
'Florida Marlins' : 2,
'Arizona Diamondbacks' : 1,
'Anaheim Angels' : 1,
'San Francisco Giants' : 3,
'Houston Astros' : 1,
'Washington Nationals' : 1}

要排序的字典是按值降序排列的。
然后:

sorted_wins = {i:matches_won[i] for i in sorted(matches_won,key = lambda x:matches_won[x],reverse = True)}

或者,另一种方法是:

dict(sorted(matches_won.items(), key=lambda item: item[1],reverse = True))

这给出了:

{'New York Yankees': 27,
'St. Louis Cardinals': 11, 
'Boston Red Sox': 8, 
'New York Giants': 5, 
'Pittsburgh Pirates': 5, 
'Philadelphia Athletics': 5, 
'Cincinnati Reds': 5, 
'Los Angeles Dodgers': 5, 
'Detroit Tigers': 4, 
'Oakland Athletics': 4, 
'Chicago White Sox': 3, 
'Chicago Cubs': 3, 
'Baltimore Orioles': 3, 
'San Francisco Giants': 3, 
'Cleveland Indians': 2, 
'New York Mets': 2, 
'Philadelphia Phillies': 2, 
'Kansas City Royals': 2, 
'Minnesota Twins': 2, 
'Toronto Blue Jays': 2, 
'Florida Marlins': 2, 
'Boston Americans': 1, 
'World Series Not Played in 1904': 1, 
'Boston Braves': 1, 
'Washington Senators': 1, 
'Brooklyn Dodgers': 1, 
'Milwaukee Braves': 1, 
'World Series Not Played in 1994': 1, 
'Atlanta Braves': 1, 
'Arizona Diamondbacks': 1, 
'Anaheim Angels': 1, 
'Houston Astros': 1, 
'Washington Nationals': 1}
相关问题