【问题标题】:Printing movies in the same year in the proper order按正确顺序打印同一年的电影
【发布时间】:2020-02-10 12:31:58
【问题描述】:

我的所有代码都已完成并且运行正常我的问题是,当我在同一年打印两部电影时,打印顺序错误,我需要它们按列表顺序打印。这只发生在同一年的电影印刷中,谁能看到我的代码中的问题出在哪里?

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 应该打印:

声望,克里斯托弗·诺兰

逝者,马丁·斯科塞斯

我的指纹:

逝者,马丁·斯科塞斯

声望,克里斯托弗·诺兰

【问题讨论】:

    标签: python loops dictionary printing nested-loops


    【解决方案1】:

    here 所述,您无法影响字典中项目的顺序。

    您可以做的是获取排序的键列表并对其进行迭代。像这样:

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

    【讨论】:

      【解决方案2】:

      python 3.7 之前,字典不记得插入顺序,但您可以使用orderedDict。或者,如果在较早的 python 版本中工作无关紧要,您可以切换到 python 3.7

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-28
        相关资源
        最近更新 更多