Skip to content
Advertisement

Calculate delta in dictionary of dictionary

I have a dictionary of dictionaries which hold list of tuples like this:

JavaScript

I would like to calculate the delta of the third items (which their first two items are identical) in each tuple inside of the dictionaries, between each week (e.g., week1 and week2,.. week19 and week20)and put them as new dictionaries in the main dictionary. So my desired outcome could be like this:

JavaScript

Advertisement

Answer

To get each pair of weeks, you can use a pairwise iterator, which you can look at the itertools recipes to see the implementation; it looks like this:

JavaScript

Then you can use that on an entry of your dictionary:

JavaScript

Use this to loop through your dictionary:

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