Screen Link: https://app.dataquest.io/m/1000331/python-data-analysis-basics-practice-problems/7/analyzing-game-sales-6
DQ Code (answer):
import csv
# We solved the problem by using functions to organize our code
# but this was not a requirement, you can solve in another way.
def read_games(filename):
"""
Auxiliary function to read a CSV
"""
with open(filename) as f:
reader = csv.reader(f)
rows = list(reader)
return rows[1:]
def group_games_by_col(games, col_index):
"""
Given a column index, creates a dictionary
where the keys are the values on that column
and each value is the list of all games with
the same value on that column.
"""
games_per_col = {}
for game in games:
key = game[col_index]
if not game[col_index] in games_per_col:
games_per_col[key] = []
games_per_col[key].append(game)
return games_per_col
def compute_publisher_sales_per_zone(games):
"""
Compute the sales of each publisher by zone.
"""
games_per_publisher = group_games_by_col(games, 4)
publisher_sales_by_zone = {}
for publisher in games_per_publisher:
publisher_sales_by_zone[publisher] = {zone: 0 for zone in zones_index}
for games in games_per_publisher[publisher]:
for zone in zones_index:
zone_sales = games[zones_index[zone]]
publisher_sales_by_zone[publisher][zone] += float(zone_sales)
return publisher_sales_by_zone
zones_index = {
'NA_Sales': 5,
'EU_Sales': 6,
'JP_Sales': 7,
'Other_Sales': 8
}
games = read_games('game_sales.csv')
publisher_sales_by_zone = compute_publisher_sales_per_zone(games)
sales_ubisoft = publisher_sales_by_zone['Ubisoft']
most_sales_zone_ubisoft = max(sales_ubisoft, key=sales_ubisoft.get)
I have solved this exercise, but my code is not so elegant. I tried to understand the answer provided by DQ for this exercise. I am struggling to understand the zone_index
part (see below):
for publisher in games_per_publisher:
publisher_sales_by_zone[publisher] = {zone: 0 for zone in zones_index}
for games in games_per_publisher[publisher]:
for zone in zones_index:
zone_sales = games[zones_index[zone]]
publisher_sales_by_zone[publisher][zone] += float(zone_sales)
zones_index = {
'NA_Sales': 5,
'EU_Sales': 6,
'JP_Sales': 7,
'Other_Sales': 8
}
Could someone explain to me this part?
Thanks in advance!