Skip to content
Advertisement

summing values of dictionary “grouping by” keys-value in another dictionary

I have a dictionary that looks like this

JavaScript

while another dictionary that looks like this:

JavaScript

what I want to do is to create a new dictionary that looks like this:

JavaScript

so basically “group by” the elements in x according to the values in y and sum them. for instance, in y ‘b’,’c’,’d’, belong to ‘B’ so in z we will have B = 3+3+5 = 11,

Advertisement

Answer

JavaScript

Prints:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement