Dictonary and list help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Dictonary and list help (/thread-14623.html) |
Dictonary and list help - Btoulouse - Dec-09-2018 Hey guys, I'm having tons of trouble getting this to work right. So for whatever reason, not all of the years are matching correctly and I cant figure out why. Some work fine, and others don't. # Build a dictionary containing the specified movie collection names = ['Munich, Steven Spielberg', 'The Prestige, Christopher Nolan' , 'The Departed, Martin Scorsese' , 'Into the Wild, Sean Penn' , 'The Dark Knight, Christopher Nolan' , 'Mary and Max, Adam Elliot' , "The King's Speech, Tom Hooper" , 'The Artist, Michel Hazanavicius' , 'The Help, Tate Taylor' , 'Argo, Ben Affleck' , '12 Years a Slave, Steve McQueen' , 'Birdman, Alejandro G. Inarritu' ,'Spotlight, Tom McCarthy' , 'The BFG, Steven Spielberg'] movies = { 2005 : names[0], 2006 : names[1:3], 2007 : names[3], 2008 : names[4], 2009 : names[5], 2010 : names[6], 2011 : names[7:9], 2012 : names[9], 2013 : names[10], 2014 : names[11], 2015 : names[12], 2016 : names[13] } elif sel == 't': names.sort() for name in names: for key, value in movies.items(): if name == 'The Departed' or name == 'The Prestige': year = 2006 elif name == 'The Help' or name == 'The Artist': year = 2011 elif name == value: year = key temp = name.find(',') print(name[0:temp] + ':' ) print('\t' + name[temp + 2:len(name)], year) print('')This is the output i'm getting MENU Sort by: y - Year d - Director t - Movie title q - Quit Choose an option: 12 Years a Slave: Steve McQueen 2013 Argo: Ben Affleck 2012 Birdman: Alejandro G. Inarritu 2014 Into the Wild: Sean Penn 2007 Mary and Max: Adam Elliot 2009 Munich: Steven Spielberg 2005 Spotlight: Tom McCarthy 2015 The Artist: Michel Hazanavicius 2015 The BFG: Steven Spielberg 2016 The Dark Knight: Christopher Nolan 2008 The Departed: Martin Scorsese 2008 The Help: Tate Taylor 2008 The King's Speech: Tom Hooper 2010 The Prestige: Christopher Nolan 2010 Notice they years for 'The Artist', 'The Departed', 'The help', and 'the Prestige' is wrong. Im stuck! Any help would be appreciated! RE: Dictonary and list help - Larz60+ - Dec-09-2018 Hello, Please post enough code so that we can run it. Not sure exactly what you are trying to accomplish. Knowing the goal would be helpful. RE: Dictonary and list help - Btoulouse - Dec-09-2018 This should be enough to run it. So the goal is to organize the list of movies alphabetically by title, while matching up the director and the year the film was made. names = ['Munich, Steven Spielberg', 'The Prestige, Christopher Nolan' , 'The Departed, Martin Scorsese' , 'Into the Wild, Sean Penn' , 'The Dark Knight, Christopher Nolan' , 'Mary and Max, Adam Elliot' , "The King's Speech, Tom Hooper" , 'The Artist, Michel Hazanavicius' , 'The Help, Tate Taylor' , 'Argo, Ben Affleck' , '12 Years a Slave, Steve McQueen' , 'Birdman, Alejandro G. Inarritu' ,'Spotlight, Tom McCarthy' , 'The BFG, Steven Spielberg'] movies = { 2005 : names[0], 2006 : names[1:3], 2007 : names[3], 2008 : names[4], 2009 : names[5], 2010 : names[6], 2011 : names[7:9], 2012 : names[9], 2013 : names[10], 2014 : names[11], 2015 : names[12], 2016 : names[13] } # Prompt the user for a year year = input('Enter a year between 2005 and 2016:\n') year = int(year) # Displaying the title(s) and directors(s) from that year if year == 2006 or year == 2011: temp = movies[year] print(temp[0]) print(temp[1]) #To diplay years with multiple movies elif year == 2005 or 2006 < year < 2011: print(movies[year]) elif 2011 < year < 2017: print(movies[year]) else: print('N/A') print('') # Display menu print('MENU') print('Sort by:') print('y - Year') print('d - Director') print('t - Movie title') print('q - Quit') print('') # Carry out the desired option: Display movies by year, sel = input('Choose an option:\n') while sel == 'y' or sel == 'd' or sel == 't' or sel == 'q': if sel == 'q': break # Sort movies by year elif sel == 'y': for year in movies.keys(): year1 = str(year) print(year1 + ':') if year == 2006 or year == 2011: temp = movies[year] print('\t' + temp[0]) print('\t' + temp[1]) print('') else: print('\t'+ movies[year]) print('') #sort by title (Having trouble matching the year) elif sel == 't': names.sort() for name in names: for key, value in movies.items(): #print('Test test tes',name) if name == 'The Prestige, Christopher Nolan': year = 2006 elif name == 'The Departed, Martin Scorsese': year = 2006 elif name == 'The Help, Tate Taylor' or name == 'The Artist, Michel Hazanavicius': year = 2011 elif name == value: year = key temp = name.find(',') print(name[0:temp] + ':' ) print('\t' + name[temp + 2:len(name)] + ',' , year) print('')I know the if,else statement isn't the best way to go about this. if name == 'The Departed' or name == 'The Prestige': year = 2006 elif name == 'The Help' or name == 'The Artist': year = 2011 elif name == value: year = keyThis is just so I had working code to turn in RE: Dictonary and list help - Larz60+ - Dec-10-2018 You should restructure your dictionary similar to: movie_dict = { '2006': { 'The Prestige': { 'director': 'Christopher Nolan', # then can add additional information about movie, like: 'actors': { 'stars': { 'Hugh Jackman': { 'role': 'Robert Angier - rival stage magician in London at the end of the 19th century', '...': '...' }, 'Christian Bale': { 'role': 'Alfred Borden' } } } }, 'The Departed': { 'director': 'Martin Scorsese', 'writer': 'William Monahan', 'actors': { '...': '...' } } } }'...' just indicates where you would add additional information the way that you have it sort of defeats the purpose of having a dictionary in the first place. If written like above, you can add and remove information very easily, and access any way you wish. for sorting by value, refer to this post: https://python-forum.io/Thread-sorting-nested-dict-according-to-values and this article: https://www.blog.pythonlibrary.org/2012/06/19/python-201-how-to-sort-a-dictionary-by-value/ Additional - post edit you can save a large dictionary as a JSON file, and read it in simply wherever needed. I usually create my dictionaries in a separate script, save as JSON and then read a=in as needed, here's code (haven't tested, but I think it's correct) to do that: import json def save_json(dictname, filename): with open(filename, 'w') as fp: json.dump(dictname, fp) def load_json(dictname, filename): dictname = json.load(fp) |