-1

If I have an array of strings which I have generated with a for loop:

A = ['a1','a2','a3'] And I have also defined the following variables:

 a1 = 3;
 a2 = 4;
 a3 = 5;

How do I make an array of these variables starting from A? That is, I don't want to copy and past all the variables by hand, but instead I want to convert these strings into the variables that are already defined with the strings as names.

  • Why do you want to do this? Also, have you tried anything, done any research? – AMC Mar 09 '20 at 23:08
  • 1
    Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – AMC Mar 09 '20 at 23:10
  • I want to do it because I have a very long list of variables with names that have a numbering in them (like a1, a2, a3 etc.) and I want to turn all of that into an array. – Ruben Lier Mar 09 '20 at 23:27
  • _I want to turn all of that into an array_ Don't you already have a list, though? – AMC Mar 09 '20 at 23:38
  • sorry when is said list I just meant a lot of variables defined below each other – Ruben Lier Mar 10 '20 at 08:52
  • You still haven't said why that's necessary, though, right? – AMC Mar 10 '20 at 17:03
  • I don't really understand what you mean with necessary, I could indeed just take all the data and copy and paste it into an array, but I was hoping that there was a trick to automatically put it into an array using the orderly numbered names of the variables. But I have now found a different solution by making a dictionary of the data starting from what @Mark Snyder said, which then translates the generated array of strings into the array with the data. – Ruben Lier Mar 10 '20 at 19:10
  • Sorry if my comment wasn't clear, I'm just confused as to what your data actually looks like, and why you defined a bunch of variables like that in the first place. I think the abstract example is throwing me off. – AMC Mar 10 '20 at 19:15
  • I am writing a program that turns mathematica equations that are in the standard LateX form that mathematica generates into the LateX form that I want. Its just a lot of variables defined as strings of the old LateX form and variables defined as strings of the new LateX form. I first put these variables into an array by hand but then I realized that it would be better to do it automatically because that would make changing things easier but at that point I had already typed out a lot so I couldn't really start from scratch. – Ruben Lier Mar 10 '20 at 19:24

1 Answers1

0

Use a dict instead, with the "variable names" as keys in the dict.

A = ['a1','a2','a3']
my_dict = dict()
for item in A:
    my_dict[item] = value_gen(item)
Mark Snyder
  • 1,635
  • 3
  • 12