Dictionary/List Homework - 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: Dictionary/List Homework (/thread-14682.html) |
Dictionary/List Homework - ImLearningPython - Dec-12-2018 Hello all!! I am currently working on a homework assignment where I need to get information from a dictionary based off of a list. Example: dict = {'title':[year, 'name'],....etc list = ['name','name','name'].....etc What I need to do is get the name from list to match the name in the dictionary and then print the values from the dictionary. movies = {"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"]} options = input('Choose a sort option: \n') director_list = [] if options == 'd': for key in movies: i = 1 while i < len(movies[key]): director_list.append(movies[key][i]) i += 2 set_director = sorted(set(director_list)) for key in set_director: print('%s:\n' % key) i = 0 for key, value in sorted(movies.items(), key=lambda item: (item[0], item[1])): i = 1 while i < len(movies[key]): print("\t" + key + ',',movies[key][0]) i += 2 RE: Dictionary/List Homework - DeaD_EyE - Dec-12-2018 def get_author(data, search_author): # using here argument unpacking for movie, (year, author) in data.items(): if search_author == author: print(movie, year, author) get_author(movies, 'Steven Spielberg')Without argument unpacking: def get_author(data, search_author): # using here tuple unpacking for movie, value in data.items(): year = value[0] author = value[1] if search_author == author: print(movie, year, author) get_author(movies, 'Steven Spielberg')If you want to sort them by X, use the sorted builtin function together with the key argument.def by_author(item): ''' Returns the author of an item ''' return item[1][1] def by_year(item): ''' Returns the year of an item ''' return item[1][0] def by_movie(item): ''' Returns the movie of an item ''' return item[0] def get_author_sorted(data, search_author, sort_by): for movie, (year, author) in sorted(data.items(), key=sort_by): if search_author == author: print(movie, year, author) get_author_sorted(movies, 'Steven Spielberg', by_year) RE: Dictionary/List Homework - ImLearningPython - Dec-12-2018 Thank you RE: Dictionary/List Homework - ImLearningPython - Dec-12-2018 While working on this, I have it working as it should with one exception. Under the dictionary, there is two list that has 4 values instead of 2 values. When I run the program it isn't printing the right values together for just those two year. I have messed with this for days and can't seem to figure out how to get it to print correctly. movies = {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']} options = input('Choose a sort option: \n') title_list = [] if options == 't': for key in movies: i = 0 while i < len(movies[key]): title_list.append(movies[key][i]) i += 2 set_title = sorted(set(title_list)) for title in set_title: print('%s:' % title) for year, value in sorted(movies.items()): if title in value: print('\t%s, %s' % (value[value.index(title)-1], year)) print() RE: Dictionary/List Homework - woooee - Dec-12-2018 Your dictionary is messed up. Should be separate sub-lists 2011: [['The Artist', 'Michel Hazanavicius'], ['The Help', 'Tate Taylor']], RE: Dictionary/List Homework - ImLearningPython - Dec-12-2018 (Dec-12-2018, 05:31 PM)woooee Wrote: Your dictionary is messed up. Should be separate sub-lists2011: [['The Artist', 'Michel Hazanavicius'], ['The Help', 'Tate Taylor']], If I change that then none of the program runs correctly. RE: Dictionary/List Homework - woooee - Dec-12-2018 Not enough info to go further. Is this dictionary done, or do you want to add to it later? RE: Dictionary/List Homework - ImLearningPython - Dec-12-2018 It's done. I have to sort the dictionary based off of user input. q = quit d = director y = year t = title I have the year code and I have the director code. The title sort isn't working out so well. Director and title have to be sorted in alphabetical order. Which is why I made a list from the values in the dictionary so I could sort them and then use them as a value to reference the dictionary key and elements. RE: Dictionary/List Homework - nilamo - Dec-12-2018 (Dec-12-2018, 08:04 PM)ImLearningPython Wrote: If I change that then none of the program runs correctly.Then fix the program. Instead of having a list of movies/titles where every other element has meaning, just make each year a list of lists, as was shown. It makes sense logically, looks better, is easier to update, and the code that parses it will still be simple. RE: Dictionary/List Homework - woooee - Dec-13-2018 (Dec-12-2018, 08:41 PM)nilamo Wrote: Instead of having a list of movies/titles where every other element has meaning, just make each year a list of lists, as was shown. It makes sense logically, looks better, is easier to update, and the code that parses it will still be simple. i.e 2005: [['Munich', 'Steven Spielberg']], 2006: [['The Prestige', 'Christopher Nolan'], ['The Departed', 'Martin Scorsese']], etc. |