How to assign data array to a variable whos name is stored within another array?
<p>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.</p>

<p>For example:</p>

<pre><code>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)
</code></pre>

<p>The data arrays <code>trX_z.data</code> contained in the first array <code>r1_radial_ZZ</code> 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 <code>cCoh_XX</code> and <code>Var_XX</code> from the first array <code>r1_radial_ZZ</code>.</p>

<p>The code I currently have to do this is as follows:</p>

<pre><code>            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])
</code></pre>

<p>Where the function <code>scpw.coherency_ensemble</code> returns some three arrays, the first of which I assign to the variable <code>cCoh_ens_freq</code>, 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 <code>r1_radial_ZZ</code>.  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 <code>cCoh_AB</code> I get an error saying the local variable was referenced before assignment.</p>

<p>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? </p>

<p><strong>UPDATE</strong></p>

<p>Here is the simplified code example for my question:</p>

<pre><code>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()
</code></pre>

<p>Essentially, I want to be able to call the variables <code>cCoh_AB</code> and <code>Var_AB</code> and have them return the values "2" and "4" respectively.</p>

<p>The answer was to use the variable names in <code>first_array</code> to generate keys in an empty dictionary to which I could assign the data generated by the <code>calculation()</code> function:</p>

<pre><code>def calculation(x, y):

    x2 = 2*x
    y2 = 2*y

    return "double", x2, y2

def dict_example():

    cCoh_dict = {}

    first_array = np.array([["cCoh_AB", "Var_AB", 1, 2]], dtype=object)

    for i in range(first_array.shape[0]):
        type, cCoh_dict[first_array[i, 0]], cCoh_dict[first_array[i, 1]] = calculation(first_array[i, 2], first_array[i, 3])
</code></pre>

<p>Then the values for the keys assigned with the variable names in <code>first_array</code> are as expected:</p>

<pre><code>print first_array[0,0]
&gt;&gt;&gt; cCoh_AB

print cCoh_dict["cCoh_AB"]
&gt;&gt;&gt; 2

print cCoh_dict["Var_AB"]
&gt;&gt;&gt; 4
</code></pre>

python numpy