1 #This is a challenging one! The output will be very long as 2 #you\'ll be work
ID: 3708255 • Letter: 1
Question
1 #This is a challenging one! The output will be very long as 2 #you'll be working on some pretty big dictionaries. We don't 3 #expect everyone to be able to do it, but it's a good chance 4 #to test how far you've come ! 6 #write a function called stars that takes in two 7 #dictionaries: 9 #-movies: a dictionary where the keys are movie titles and 10 # the values are lists of major performers in the movie. For 11 # example: movies("The Dark Knight"] - ["Christian Bale", 12 # "Heath Ledger", "Maggie Gyllenhall", "Aaron Eckhart"] 13 #-tvshows: a dictionary where the keys are TV show titles 14 # and the values lists of major performers in the show 15 # For example: tvshows [ "Community"] [ "Joel McHale", "Alison 16 # Brie", "Danny Pudi", "Donald Glover ", "Yvette Brown "] 17# 18 #The function stars should return a new dictionary. The keys 19 #of the new dictionary should be the performers' names, and 20 #the values for each key should be the list of shows and 21 #movies in which that performer has appeared. Sort the shows 22 #and movies alphabetically. 23 24 25 #write your function here! 26 27 28 29 #Below are some lines of code that will test your function. 30 #You can change the value of the variable(s) to test your 31 #function with different inputs. 32 # 33 #If your function works correctly, this will originally 34 #print (although the order of the keys may vary): 35 # 36 #{'Portia de Rossi': ['Arrested Development' ], 'will Ferrell': [ 'The Lego Movie'], 'Yvette Brown':'Community'l Rebel Wilson' [How to Be Single'1Danny Pudi:Community'1 Elizabeth Banks':'30 Rock'The Lego Movie', "Alec Baldwin':30 Rock', Alison Brie': [ "Community', How to Be Single',The Lego Movie', Tina Fey:[30 Rock' Dakota Johnson [ 'How to Be Single',Joel McHale':'Community' Jack McBrayer'[30 Rock', Tracy Morgan': [30 Rock'] Donald Glover': ['Community', 'Will Arnett''Arrested Development' The Lego Movie', Jason Bateman': 'Arrested Development'1 37 38 movies -("How to Be Single": ["Alison Brie", "Dakota Johnson", 39 40 41 42 tvshows -("Community": ["Alison Brie", "Joel McHale", 43 "Rebel Wilson" ], " The Lego Movie": ["Will Arnett", "Elizabeth Banks", Alison Brie", "Will Ferrell" 1 "Danny Pudi", "Yvette Brown", "Donald Glover"], "30 Rock'' : ?"Tina Fey ", "Tracy Morgan ", "Jack McBrayer" 45 4 6 47 48 49 print (stars (movies, 50 "Alec Baldwin", "Elizabeth Banks"1, "Arrested Development": ["Jason Bateman", "Will Arnett", "Portia de Rossi" ]} tvshows))Explanation / Answer
def stars(movies, tvshows): d = {} for show, performers in tvshows.items(): for performer in performers: if performer not in d: d[performer] = [] d[performer].append(show) for movie, performers in movies.items(): for performer in performers: if performer not in d: d[performer] = [] d[performer].append(movie) for performer, lst in d.items(): lst.sort() return d movies = {"How to Be Single": ["Alison Brie", "Dakota Johnson", "Rebel Wilson"], "The Lego Movie": ["Will Arnett", "Elizabeth Banks", "Alison Brie", "Will Ferrell"]} tvshows = {"Community": ["Alison Brie", "Joel McHale", "Danny Pudi", "Yvette Brown", "Donald Glover"], "30 Rock": ["Tina Fey", "Tracy Morgan", "Jack McBrayer", "Alec Baldwin", "Elizabeth Banks"], "Arrested Development": ["Jason Bateman", "Will Arnett", "Portia de Rossi"]} print(stars(movies, tvshows))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.