首页 > 解决方案 > 在 Python 上用这段代码创建一个字典,另一个排序的字典谢谢

问题描述

因此,我已经完成了所有工作。我无法弄清楚如何从我创建的字典中制作一个排序字典。这本词典应该有赢得比赛最多的球队为第一名,依此类推。

这是我创建的第一本字典 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()

尝试我已经得到的方法我仍然只有这本字典

字典

这是字典:

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}

标签: pythonwindowsdictionary

解决方案


让我们假设:

 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}

推荐阅读