Hi,
I’ve been really enjoying doing the practice problems, but I’ve come across one that I haven’t fully been able to understand.
This is the question:
- Create a function,
avg_group
, with the following features: - The first argument is a dictionary that follows the schema
{column_name: {index: value}}
. - The second argument is one of the string keys in the first argument.
- Returns a new dictionary where:
- They keys are the values in the entry of the first argument at the second argument.
- The values are lists containing the averages for
total_bill
,tip
andsize
in this order.
And this is the answer, along with comments about what’s confusing me:
def get_avgs(d, value, indices):# what is the purpose of the indices parameter
sums = [0 for _ in range(3)]
for idx in indices:
sums[0] += d["total_bill"][idx]
sums[1] += d["tip"][idx]
sums[2] += d["size"][idx]
return list(map(lambda x: x/len(indices), sums))
def avg_group(d, col):
data = d[col] # | Whats is the purpose of these 3 lines of code
groups = list(set(d[col].values())) # |
groups.sort() # |
group_by = {}
for key in groups:
indices = [k for k,v in d[col].items() if v == key] #what is the purpose of this
group_by[key] = get_avgs(d, key, indices)
return group_by
Screen Link:
https://app.dataquest.io/m/1020/dictionaries/7/average-by-group