0

I'm trying to condense some repetitive code by setting up a for loop to define variable names. I get that python doesn't like having operators on the left side of equations, but are there any workarounds for adding strings together for the sake of defining a variable name that will later be referenced?

cases     = ['case1','case2', 'case3']
condition = ['con1', 'con2']
var       = ['var1', 'var2', 'var3']
for i in cases :
    for j in condition :
        for k in var :
            filename = i+j+k+'.txt'
            i+j+k = np.loadtxt(filename)
            plt.plot(x, i+j+k)
plt.show

I'm thinking my best option would option would be set up a matrix in the loop as

value[i,j,k] = np.loadtxt(filename)
plt.plot(x, value[i,j,k])

but that might lead to bigger headaches in the future so I'm trying to avoid if possible.

jtolento
  • 3
  • 4
  • You're trying to make `3 * 2 * 3` different named variables? Yeah, no, do what you suggested as the best option. Or in the code as written, just assign to a temporary variable with a fixed name, I'm entirely unclear on why you think you need different names for each one. The code as written could just do `plt.plot(x, np.loadtxt(f'{i}{j}{k}.txt'))` without needing a temporary variable at all. – ShadowRanger Sep 07 '22 at 20:09
  • Don't do it. See the answers to this [question](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – quamrana Sep 07 '22 at 20:10
  • You don't need a different temp-variable name in each iteration, anyway. Just use something like `t = np.loadtxt(filename); plt.plot(x, t)` if you really want a temporary variable. – chepner Sep 07 '22 at 20:13
  • @chepner OP mentions they want to access these variables again later. – JRose Sep 07 '22 at 20:15
  • @chepner I think I do need a dynamic variable. This is a very simplified case of what I'm trying to do to highlight the problem I'm trying to work on. But I need to store the variables to do further analysis later in the script (for example subtracting [case1_con1_var1) - [case1_con1_var2] ) I wouldn't be able to do this if I overwrite the variable with a fixed variable at each loop – jtolento Sep 07 '22 at 20:34

2 Answers2

1

Creating variable names based on strings is not allowed. Using a dictionary to store the values with your custom string is the closest thing you will get.

Also, you have a mistake here: filename = i+j+k'.txt' , you need to add the .txt string to the other strings like this filename = i+j+k+'.txt'

cases     = ['case1','case2', 'case3']
condition = ['con1', 'con2']
var       = ['var1', 'var2', 'var3']
case_condition_variables = dict()

for i in cases :
    for j in condition :
        for k in var :
            filename = i+j+k+'.txt'
            case_condition_variables[i+j+k] = np.loadtxt(filename)
            plt.plot(x, case_condition_variables[i+j+k])

print(case_condition_variables) # will show you all the name - value pairs
JRose
  • 1,382
  • 2
  • 5
  • 18
  • 1
    Looks like that'll work! Thank you ! – jtolento Sep 07 '22 at 20:20
  • @jtolento In this case, the dictionary isn't even needed. You don't even need aa dynamic variable name. Instead, you are building the file name dynamically. As for the plotting, just do `data = np.loadtxt(filename)` and `plt.plt(x, data)`. – Code-Apprentice Sep 07 '22 at 20:23
  • @Code-Apprentice "defining a variable name that will later be referenced" OP wishes to refer to this exact "variable" again later to retrieve the contents of a file, which I believe qualifies for use of a dictionary – JRose Sep 07 '22 at 20:31
  • 1
    @Code-Apprentice I think I do need a dynamic variable. This is a very simplified case of what I'm trying to do to highlight the problem I'm trying to work on. But I need to store the variables to do further analysis later in the script (for example subtracting [case1_con1_var1) - [case1_con1_var2] ) I wouldn't be able to do this if I overwrite the variable with a fixed variable at each loop – jtolento Sep 07 '22 at 20:32
  • @jtolento That makes sense. In that case, using an appropriate data structure, such as a dict seems like a good way to go. – Code-Apprentice Sep 08 '22 at 03:39
0

You are making this infinitely more complex then necessary. You don't need to create a variable name from strings. Instead, just use a fixed variable:

            data = np.loadtxt(filename)
            plt.plot(x, data)

On the other hand, you can build a filename from strings:

filename = i + j + k + '.txt'

The only thing you are missing here is the + to correctly concatenate the '.txt' extension to the filename.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268