在同一年以适当的方式印刷电影

2024-10-03 09:11:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有我所有的代码完成,它运行正常,我的问题是,当我打印两部电影,为同一年的打印在错误的顺序,我需要他们打印的顺序列表。这只发生在同一年的电影打印中,有人能看到我的代码中的问题在哪里吗

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年应印刷:

威望,克里斯托弗·诺兰

逝者马丁·斯科塞斯

我的指纹:

逝者马丁·斯科塞斯

威望,克里斯托弗·诺兰


Tags: thekeyinforiftitlevaluemovie
2条回答

python 3.7之前,字典不记得插入顺序,但可以使用orderedDict。或者,如果在早期的python版本中工作并不重要,您可以切换到python3.7

如前所述here您不能影响字典中项目的顺序

不过,您可以做的是获得已排序的键列表并对它们进行迭代。像这样:

keys = list(movieCollectionDict.keys())
keys.sort()
for key in keys:
    if movieCollectionDict[key][0] == yearChoice:
        print(key + ", " + str(movieCollectionDict[key][1]))

相关问题 更多 >