I want to write a function that will operate on multiple dictionary values at once.
Specifically, I want to take the all the values of selected_keys, divide them by 1 million, and round to two decimal places. Something like this:
def scale_down_movie(movie):
selected_keys = movie['budget'], movie['budget_2013$'], movie['domgross'], movie['domgross_2013$'], movie['intgross'], movie['intgross_2013$']
for value in selected_keys:
round(value/1000000, 2)
return movie
scale_down_movie(movies[0]) is currently outputting:
{'budget': 13000000,
'domgross': 25682380.0,
'intgross': 42195766.0,
'budget_2013$': 13000000,
'domgross_2013$': 25682380.0,
'intgross_2013$': 42195766.0
}
desired output:
{'budget': 13.00,
'domgross': 25.68,
'intgross': 42.19,
'budget_2013$': 13.00,
'domgross_2013$': 25.68,
'intgross_2013$': 42.19
}
Thank you.
UPDATE:
Used this code:
def scale_down_movie(movie):
selected_keys = ('domgross_2013$', 'budget', 'budget_2013$', 'domgross', 'domgross_2013$', 'intgross', 'intgross_2013$')
for key in selected_keys:
movie[key] = round(movie[key]/1000000, 2)
return movie
Getting unexpected results.
Expected output:
scale_down_movie(parsed_movies[9])
{'binary': 'FAIL',
'budget': 130.0,
'budget_2013$': 130.0,
'clean_test': 'notalk',
'code': '2013FAIL',
'decade code': 1.0,
'domgross': 60.52,
'domgross_2013$': 60.52,
'imdb': 'tt1815862',
'intgross': 244.37,
'intgross_2013$': 244.37,
'period code': 1.0,
'test': 'notalk',
'title': 'After Earth',
'year': 2013}
Actual output:
{'year': 2013,
'imdb': 'tt1815862',
'title': 'After Earth',
'test': 'notalk',
'clean_test': 'notalk',
'binary': 'FAIL',
'budget': 0.0,
'domgross': 0.0,
'intgross': 0.0,
'code': '2013FAIL',
'budget_2013$': 0.0,
'domgross_2013$': 0.0,
'intgross_2013$': 0.0,
'period code': 1.0,
'decade code': 1.0}
SOLUTION:
def scale_down_movie(movie):
scaled_movie = movie.copy()
selected_keys = ('domgross_2013$', 'budget', 'budget_2013$', 'domgross', 'domgross_2013$', 'intgross', 'intgross_2013$')
for key in selected_keys:
scaled_movie[key] = round(scaled_movie[key]/1000000, 2)
return scaled_movie
WHAT WENT WRONG: Every time you push run, it divides by a million again, so it quickly approaches 0.
HOW TO FIX IT: Use .copy() so that you're not changing the original.
How about something like:
def scale_down_movie(movie):
selected_keys = ('domgross_2013$', 'budget', 'budget_2013$', 'domgross', 'domgross_2013$', 'intgross', 'intgross_2013$')
for key in selected_keys:
movie[key] = round(movie[key]/1000000, 2)
return movie
However this may not return the values you're expecting as you are doing integer division. If you replace movie[key]/1000000
with movie[key]/float(1000000)
it should work as expected.
If you need a new dictionary
def scale_down_movie(movie):
selected_keys = {'domgross_2013$', 'budget',
'budget_2013$', movie'domgross', 'domgross_2013$',
'intgross', 'intgross_2013$'}
return { k: v/1000000., 2) for k, v in selected_keys.items if k in selected_keys}
You're returning the wrong value:
def scale_down_movie(movie):
selected_keys = movie['domgross_2013$'], movie['budget'],
movie['budget_2013$'], movie['domgross'], movie['domgross_2013$'],
movie['intgross'], movie['intgross_2013$']
for value in selected_keys:
round(value/1000000, 2)
return selected_keys
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.