用嵌套列表对字典进行排序

时间:2018-10-09 22:28:44

标签: python sorting dictionary nested-loops

我有一本带有嵌套列表的字典。我需要按年份,职务和导演进行排序,并将其打印为

year:
   title director
etc. 

我当前遇到的问题(尚未进入排序)是,当尝试从键中打印列表时(对于嵌套列表的2006和2011),它将打印两个单独的2006和2011。我的代码:

movie_dictionary = {
'2005':[['Munich','Steven Spielberg,']],
'2006':[['The Prestige','Christopher Nolan,'],['The            
Departed,','Martin Scorsese']],
'2007':[['Into the Wild,','Sean Penn']],
'2008':[['The Dark Knight,','Christopher Nolan']],
'2009':[['Mary and Max,','Adam Elliot']],
'2010':[["The King's Speech,",'Tom Hooper']],
'2011':[
    ['The Artist,','Michel Hazanavicius'],
    ['The Help,','Tate Taylor']
],
'2012':[['Argo,','Ben Affleck']],
'2013':[['12 Years a Slave,','Steve McQueen']],
'2014':[['Birdman,','Alejandro G. Inarritu']],
'2015':[['Spotlight,','Tom McCarthy']],
'2016':[['The BFG,','Steven Spielberg']]
}

# Prompt the user for a year 
year = input('Enter a year between 2005 and 2016:\n')
# Displaying the title(s) and directors(s) from that year
movie_display = movie_dictionary.get(year,'N/A')

if movie_display == 'N/A':
    print('N/A')
else:
    for movie in movie_display:
        print(movie[0],movie[1])

# Display menu
print()
print("MENU")
print("Sort by:\n"
    "y - Year\n"
    "d - Director\n"
    "t - Movie title\n"
    "q - Quit\n")

user_input = input('Choose an option:\n').lower().strip()

if user_input == 'q':
    exit()
elif user_input == 'y':
    for year, movie in sorted(movie_dictionary.items()):
        for movies, director in movie:
            print(year+':\n', str(movies), str(director))
elif user_input == 'd':
    print()
elif user_input == 't':
    print()

输出为:

Enter a year between 2005 and 2016:
The Artist, Michel Hazanavicius
The Help, Tate Taylor

MENU
Sort by:
y - Year
d - Director
t - Movie title
q - Quit

Choose an option:
2005:
 Munich Steven Spielberg,
2006:
 The Prestige Christopher Nolan,
2006:
 The Departed, Martin Scorsese
2007:
 Into the Wild, Sean Penn
2008:
 The Dark Knight, Christopher Nolan
2009:
 Mary and Max, Adam Elliot
2010:
 The King's Speech, Tom Hooper
2011:
 The Artist, Michel Hazanavicius
2011:
 The Help, Tate Taylor
2012:
 Argo, Ben Affleck
2013:
 12 Years a Slave, Steve McQueen
2014:
 Birdman, Alejandro G. Inarritu
2015:
 Spotlight, Tom McCarthy
2016:
 The BFG, Steven Spielberg

我希望将2011年和2006年包装成两个标题。还有关于排序的建议吗?

2 个答案:

答案 0 :(得分:1)

在输出循环中,跟踪年份。仅当您获得新的一年时,才打印年份标题:

curr_year = -1000
for year, movie in sorted(movie_dictionary.items()):
    for movies, director in movie:
        if curr_year != year:
            print(year+':')
            curr_year = year

        print(str(movies), str(director))

输出:

2005:
Munich Steven Spielberg,
2006:
The Prestige Christopher Nolan,
The Departed, Martin Scorsese
2007:
Into the Wild, Sean Penn
2008:
The Dark Knight, Christopher Nolan
2009:
Mary and Max, Adam Elliot
2010:
The King's Speech, Tom Hooper
2011:
The Artist, Michel Hazanavicius
The Help, Tate Taylor
2012:
Argo, Ben Affleck
2013:
12 Years a Slave, Steve McQueen
2014:
Birdman, Alejandro G. Inarritu
2015:
Spotlight, Tom McCarthy
2016:
The BFG, Steven Spielberg

要进行排序,请遵循发布准则:搜索“ Python排序教程”,然后从中学到什么。

答案 1 :(得分:0)

您可以使用其他结构化循环来处理

elif user_input == 'y':
    for k in movie_dictionary:
        print('{}:'.format(k))
        for i in movie_dictionary[k]:
            print('  {} {}'.format(*i))
2005:
  Munich, Steven Spielberg
2006:
  The Prestige, Christopher Nolan
  The Departed, Martin Scorsese
2007:
  Into the Wild, Sean Penn
2008:
  The Dark Knight, Christopher Nolan
2009:
  Mary and Max, Adam Elliot
2010:
  The King's Speech, Tom Hooper
2011:
  The Artist, Michel Hazanavicius
  The Help, Tate Taylor
2012:
  Argo, Ben Affleck
2013:
  12 Years a Slave, Steve McQueen
2014:
  Birdman, Alejandro G. Inarritu
2015:
  Spotlight, Tom McCarthy
2016:
  The BFG, Steven Spielberg

在您当前的结构下,按导演排序可能会有些困难,因为它与标题存在于相同的值列表中,我认为更好的设置是具有二分位数,将二分位数作为诸如{{1 }}可以简化此过程,祝您好运!