Oct-13-2024, 06:33 AM
I have two variables with the following structures:
Using .deepcopy() is the only way that I know but as this is an iterative process, .deepcopy() function slows the code significantly down especially when O1 and O2 are large. I have tried using tuples as an immutable data structure but given the initial and internal structure and the changes made in the functions, it does not work. using tuples prevent the append() and remove() changes but not +/- operations.
I would be grateful if someone could suggest a faster way to do this.
# initialization O1 = [[] for g in range(n)] for i in range(n): O1[i] = [[] for g in range(m)] O2 = [[] for g in range(n)] for i in range(n): O2[i] = [[] for g in range(m)]Then, I input these two into an initial function where different values are appended to them as:
for i in range(n): for j in range(m): O1[i][j].append([s, as, pat, 0]) O2[i][j].append([[ol, oa, os, ot],[dl, da, ds],tpat, tp, 0])As you can see, the internal structure of the lists gets complicated, especially in the case of O2. After this value assignment to O1 and O2, they are input into several other functions. Each function needs to make a deep copy of O1 and O2 and modify the copy for its own use without changing the original O1 and O2 variables. The changes include both .remove() and .append() and also +/- of values in the internal lists. The important thing is that no matter the changes, the original O1 and O2 should not change at all. This process runs iteratively where first O1 and O2 are assigned new values and then input into several other functions and copied and edited without any change in the original O1 and O2 that were used as input.
Using .deepcopy() is the only way that I know but as this is an iterative process, .deepcopy() function slows the code significantly down especially when O1 and O2 are large. I have tried using tuples as an immutable data structure but given the initial and internal structure and the changes made in the functions, it does not work. using tuples prevent the append() and remove() changes but not +/- operations.
I would be grateful if someone could suggest a faster way to do this.