0

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.

4
  • 2
    Possible duplicate of Python convert string to variable name Commented Sep 14, 2016 at 7:28
  • I have simplified and re-written the code in a way so that I can be run by somebody looking at my question. I guess my problem is more that I want to store both a variable name with associated data (dictionary key and value) as well as a standard data array, as different elements in a numpy array. Is this possible with numpy or do I have to revert purely to using a dictionary? Ultimately, I'd like to avoid having to create any more data structures than already exist in my code. Commented Sep 15, 2016 at 2:48
  • why don't you just spit out the results to a key in a dictionary as suggested by the above? Commented Sep 15, 2016 at 4:13
  • Ok I have worked it out - thanks spectras and Gene for jump starting my brain. Answer is below. Commented Sep 15, 2016 at 4:40

1 Answer 1

0

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

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])

Then the values for the keys assigned with the variable names in first_array are as expected:

print first_array[0,0]
>>> cCoh_AB

print cCoh_dict["cCoh_AB"]
>>> 2

print cCoh_dict["Var_AB"]
>>> 4
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.