1

I have a dictionary that looks like this:

d_1 = {
    "k1":{
        "a":0.215,
        "b":350
    },
    "k2":{
        "a":1.0125,
        "b":1.1802
    }
}

I want to create a second dictionary that stores the result of 0.215/1.0125, stored in the a keys of k1 and k2 dictionaries, but I want the value to update in the same fashion that an excel sheets does.

To illustrate this, imagine you have the cell A1 = 0.215 and cell B1 = 1.0125. Then cell C1 would be A1/B1. If you update A1 or B1, the value of C1 is automatically updated.

Is there any way to create a second dictionary d_2 which key stores the result of d_1["k1"]["a"]/d_1["k2"]["a"] and updates the result dynamically, like the excel example? And if its not possible, what is the closest thing I could do to achieve this?


EDIT:

The desired solution I'm looking is something similar to having shallow copies of parts of d_1, like d_1["k1"]["a"]/d_1["k2"]["a"]. I know that if I have two dictionaries and one is the copy of the other, if I change the value of one key-value pair in the first dictionary, the second dictionary should also change. To get a "hard" copy of the first dictionary deepcopy should be used.

Can something like a shallow copy of the dictionary but applying the mathematical operation be done or its not possible?

xerac
  • 147
  • 8

1 Answers1

0

from my experience , it's a little bit hard to automatically update values , if you want to should set up a listener to know whether u changed the value of a in dictionary k1 or b in k2 dictionary

I might suggest you the following answers for the " how to listen on values change in a dict " question here

variables are stored in the memory so to detect the values changing you need a listener to compare the old dict and the new one

  • Hi, thank you for your answer. I'm looking more for an automated update of the value rather than listening to changes in the values. I also edited the question, so please take a look :) – xerac Dec 20 '20 at 04:24