I have a series of loops set-up which each create an array of arrays, and where the sub-array contains two variable names and two data arrays.
For example:
r1_radial_ZZ = np.array([ ["cCoh_AB", "Var_AB", trA_z.data, trB_z.data],
["cCoh_AC", "Var_AC", trA_z.data, trC_z.data],
["cCoh_AD", "Var_AD", trA_z.data, trD_z.data],
["cCoh_AE", "Var_AE", trA_z.data, trE_z.data],
["cCoh_AF", "Var_AF", trA_z.data, trF_z.data],
["cCoh_AG", "Var_AG", trA_z.data, trG_z.data] ], dtype=object)
The data arrays trX_z.data contained in the first array r1_radial_ZZ are then processed with another function which returns two more data arrays, the coherencey and variance, for which I want to assign the variable names using the strings cCoh_XX and Var_XX from the first array r1_radial_ZZ.
The code I currently have to do this is as follows:
for i in range(r1_radial_ZZ.shape[0]):
cCoh_ens_freq, r1_radial_ZZ[i,0], r1_radial_ZZ[i,1] = scpw.coherency_ensemble(r1_radial_ZZ[i,2], r1_radial_ZZ[i,3])
Where the function scpw.coherency_ensemble returns some three arrays, the first of which I assign to the variable cCoh_ens_freq, and the second and third arrays I want to assign with the variable names stored in the first and second elements in my first array r1_radial_ZZ. In reality, what happens is that the variable names for my processed data are wiped out when I assign the processed data back to the first array, and when I go to call, for example, the variable cCoh_AB I get an error saying the local variable was referenced before assignment.
How can I re-arrange my code so that the elements in my first array which contain the variable names as strings end up as data arrays with the given variable name after the code has run?
UPDATE
Here is the simplified code example for my question:
import numpy as np
def calculation(x, y):
x2 = 2*x
y2 = 2*y
return "double", x2, y2
def dict_example():
first_array = np.array([["cCoh_AB", "Var_AB", 1, 2]], dtype=object)
for i in range(first_array.shape[0]):
type, first_array[i, 0], first_array[i, 1] = calculation(first_array[i, 2], first_array[i, 3])
dict_example()
Essentially, I want to be able to call the variables cCoh_AB and Var_AB and have them return the values "2" and "4" respectively.