我已经完成所有代码,并且可以正常运行,我的问题是,当我在同一年打印两部电影时,以错误的顺序打印时,我需要它们按照列表的顺序打印。这仅发生在同一年的电影打印中,任何人都可以看到我的代码中的问题在哪里吗?
def main():
movieCollectionDict = {"Munich":[2005, "Steven Spielberg"],
"The Prestige": [2006, "Christopher Nolan"],
"The Departed": [2006, "Martin Scorsese"],
"Into the Wild": [2007, "Sean Penn"],
"The Dark Knight": [2008, "Christopher Nolan"],
"Mary and Max": [2009, "Adam Elliot"],
"The King\'s Speech": [2010, "Tom Hooper"],
"The Help": [2011, "Tate Taylor"],
"The Artist": [2011, "Michel Hazanavicius"],
"Argo": [2012, "Ben Affleck"],
"12 Years a Slave": [2013, "Steve McQueen"],
"Birdman": [2014, "Alejandro G. Inarritu"],
"Spotlight": [2015, "Tom McCarthy"],
"The BFG": [2016, "Steven Spielberg"]}
promptForYear = True
while promptForYear:
yearChoice = int(input("Enter a year between 2005 and 2016:\n"))
if yearChoice<2005 or yearChoice>2016:
print("N/A")
else:
for key, value in movieCollectionDict.items():
if value[0] == yearChoice:
print(key + ", " + str(value[1]) )
promptForYear = False
menu = "MENU" \
"\nSort by:" \
"\ny - Year" \
"\nd - Director" \
"\nt - Movie title" \
"\nq - Quit\n"
promptUser = True
first_time = True
while promptUser:
if first_time == True:
print()
first_time = False
print('MENU\n''Sort by:\n''y - Year\n''d - Director\n''t - Movie title\n''q - Quit\n')
userChoice = input("Choose an option:\n")
if userChoice == "q":
promptUser = False
elif userChoice=="y":
year_sorted = {}
for key, value in sorted(movieCollectionDict.items(), key=lambda item: (item[1], item[0])):
year = value[0]
title = key
director = value[1]
if year not in year_sorted:
year_sorted[year] = [[title, director]]
else:
year_sorted[year].append([title, director])
for year in sorted(year_sorted):
print (year,end=':\n')
movies = year_sorted[year]
for movie in sorted(movies, key = lambda x:x[0]):
print("\t"+movie[0] + ", " + movie[1])
print()
elif userChoice == "d":
director_sorted = {}
for key, value in sorted(movieCollectionDict.items(), key=lambda item: (item[1][1])):
year = value[0]
title = key
director = value[1]
if director not in director_sorted:
director_sorted[director] = [[title, year]]
else:
director_sorted[director].append([title, year])
for director in sorted(director_sorted):
print (director,end=':\n')
movies = director_sorted[director]
for movie in sorted(movies, key = lambda x:x[0]):
print("\t"+movie[0] + ", " + str(movie[1]))
print()
elif userChoice == "t":
for key, value in sorted(movieCollectionDict.items(), key=lambda item: (item[0], item[1])):
print(key,end=':\n')
print("\t" + str(value[1]) + ", " + str(value[0])+"\n")
else:
print("Invalid input")
main()
例如2006年应打印:
威望克里斯托弗·诺兰
去世的马丁·斯科塞斯(Martin Scorsese)
我的照片:
去世的马丁·斯科塞斯(Martin Scorsese)
威望克里斯托弗·诺兰
答案 0 :(得分:0)
如here所述,您不能影响字典中项目的顺序。
您可以做的是,获取已排序的键列表并遍历它们。像这样:
keys = list(movieCollectionDict.keys())
keys.sort()
for key in keys:
if movieCollectionDict[key][0] == yearChoice:
print(key + ", " + str(movieCollectionDict[key][1]))
答案 1 :(得分:0)
在python 3.7字典之前不记得插入顺序,但是您可以使用orderedDict。另外,如果在较早的python版本中工作没关系,则可以切换到python 3.7