简体   繁体   English

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

[英]sorting a dictionary with nested list

I have a dictionary with nested lists.我有一本带有嵌套列表的字典。 I need to sort by year, title and director, and print it as我需要按年份、标题和导演排序,并将其打印为

year:
   title director
etc. 

The problem I am currently having (not into the sorting yet) is that when trying to print the lists from the key, for 2006 and 2011 where there are nested lists, it prints two separate 2006 and 2011's.我目前遇到的问题(尚未进入排序)是,当尝试从键打印列表时,对于存在嵌套列表的 2006 和 2011,它会打印两个单独的 2006 和 2011。 My code:我的代码:

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()

The output is:输出是:

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

I want the 2011 and 2006 to be wrapped into one with two titles.我希望将 2011 年和 2006 年合并为一个有两个标题的版本。 Also any recommendations on sorting?还有关于排序的任何建议吗?

Within your output loop, keep track of the year.在您的输出循环中,跟踪年份。 Only when you get a new year do you print the year header:只有当你得到新的一年时,你才打印年份标题:

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))

Output:输出:

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

For sorting, please follow the posting guidelines: search for "Python sorting tutorial" and see what you learn from that.对于排序,请遵循发布指南:搜索“Python 排序教程”,看看您从中学到了什么。

You can handle this with just a different structured loop您只需使用不同的结构化循环即可处理此问题

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

With your current structure it could be a little difficult to sort by director since it exists in the same list of values as title, I think a better setup would be to have a dicitonary, with dicitonarys as values like so {'2005': {'Title': 'Munich', 'Director': 'Steven Spielberg'}} could make the process easier, goodluck!使用您当前的结构,按导演排序可能有点困难,因为它与标题存在于相同的值列表中,我认为更好的设置是使用字典,将字典作为值,例如{'2005': {'Title': 'Munich', 'Director': 'Steven Spielberg'}}可以让这个过程更容易,祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM