I have a list including some names, as example:
data_set_names=['matrix_1','matrix_2','matrix_3'].
Inside a loop, I want to use each of these names for storing the output of some computations, which are as NumPy array. I highly appreciate if someone could tell me how to do that. As I looked it up online, exec() function can be used to convert a string to a variable name, but in my case, it is not useful.
-
Would storing the matrices as values in a dictionary where the keys are the strings in the list work for you?ChrisOram– ChrisOram2021-07-30 10:34:08 +00:00Commented Jul 30, 2021 at 10:34
-
Does this answer your question? How do I create variable variables?Matthias– Matthias2021-07-30 11:17:05 +00:00Commented Jul 30, 2021 at 11:17
Add a comment
|
2 Answers
You could think about using globals() or locals() depending on your needs. Both are a dictionary of the variables you are using in your code, (global or locals) and from there you can assign a variable as a string, in your case something like:
for i in data_set_names:
globals()[i] = 'your data here'