You will need to define 2 Python functions. These functions could be used by a website tracking movie data. Each movie is represented as a dictionary containing three keys:/p>
the key "film" whose value is a string representing the name of the film;
the key "tomat" whose value is am int of its rating from a well-known website;
the key "gross" whose value is a float equalling its worldwide gross income (in million of dollars).
First, define a key selector function named gross. This function returns a result allowing the built-in sorting function to order dictionaries from the one with the lowest gross income to the dictionary with the greatest gross income.
Next, define another function named sortByGross. This function has a single parameter. This parameter will be a list of movie dictionaries. Your function will result in the list's dictionaries being ordered from the dictionary with the smallest gross to the dictionary with the greatest gross.
Examples:
gross({ "film":"WALL-E", "tomat" : 96, "gross" : 521.28}) would evaluate to 521.28
gross({ "film":"1 Day", "tomat" : 37, "gross" : 55.24}) would evaluate to 55.24
movies = [{"film":"WALL-E", "tomat" : 96, "gross" : 521.28},
{ "film":"Letters to Juliet", "tomat" : 40, "gross" : 79.18},
{ "film":"Twilight", "tomat" : 49, "gross" : 376.66},
{ "film":"The Time Traveler's Wife", "tomat" : 38, "gross" : 101.33}]
sortByGross(movies)
print(movies)
would result in the following output:
[ { 'film':'Letters to Juliet', 'tomat' : 40, 'gross' : 79.18},
{ 'film':'The Time Traveler's Wife', 'tomat' : 38, 'gross' : 101.33},
{ 'film':'Twilight', 'tomat' : 49, 'gross' : 376.66},
{ 'film':'WALL-E', 'tomat' : 96, 'gross' : 521.28}]
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
# function that returns the gross of the dictionary about film data def gross(data): return data['gross'] # function to sort data by gross def sortByGross(movies): # get all the gross data grossList = [] for g in movies: grossList.append(gross(g)) # now sort the gross as follows grossList.sort() # now add the movies data sortedMovies = [] # add each movie for g in grossList: for data in movies: if gross(data) == g: sortedMovies.append(data) break return sortedMovies # TEST if __name__ == '__main__': # TEST GROSS print(gross({"film": "WALL-E", "tomat": 96, "gross": 521.28})) print(gross({"film": "1 Day", "tomat": 37, "gross": 55.24})) # TEST SORT movies = [{"film": "WALL-E", "tomat": 96, "gross": 521.28}, {"film": "Letters to Juliet", "tomat": 40, "gross": 79.18}, {"film": "Twilight", "tomat": 49, "gross": 376.66}, {"film": "The Time Traveler's Wife", "tomat": 38, "gross": 101.33}] movies = sortByGross(movies) print(movies)
=============
You will need to define 2 Python functions. These functions could be used by a website...