var timer = setInterval(function () {
if (myParam == 'what-i-need') {
clearInterval(timer);
}
}, 200);
当我进入时,我需要帮助弄清楚为什么:
movieDict = {"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 Artist": [2011, "Michel Hazanavicius"],
"The Help": [2011, "Tate Taylor"],
"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 movieDict.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)
userChoice = input("Choose an option:\n")
if userChoice == "q":
promptUser = False
elif userChoice=="y":
year_sorted = {}
for key, value in sorted(movieDict.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(movieDict.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(movieDict.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")
2006年,电影的排序如下:
2014
y
q
但是我需要它们打印出来像:
2006:
The Departed, Martin Scorsese
The Prestige, Christopher Nolan
当我进入时:
2006:
The Prestige, Christopher Nolan
The Departed, Martin Scorsese
打印:
2008
d
q
但我需要打印:
Christopher Nolan:
The Dark Knight, 2008
The Prestige, 2006
非常感谢任何帮助,谢谢!
答案 0 :(得分:0)
此代码段:
movies = year_sorted[year]
for movie in sorted(movies, key = lambda x:x[0]):
print("\t"+movie[0] + ", " + movie[1])
强制你的年份排序按原样排出。你告诉它按字母顺序对电影进行分类,这正是它正在做的事情。当你按导演排序时,你会发生同样的事情。因此,如果您希望您的电影与其相反,那么您需要告诉它这样做
movies = year_sorted[year]
for movie in sorted(movies, key = lambda x:x[0], reverse=True):
print("\t"+movie[0] + ", " + movie[1])
您可能还打算按导演/年而不是标题排序,在这种情况下,您需要将lambda更改为lambda x:x[1]
,以便按列表中的第二个对象而不是第一个对象进行排序。