0

******Rev 00******

In Python, I'm trying to define a list of dataframes and name them after a list. These lists represent conditions and models.

ListA=['AA', 'BB', 'CC']
ListB=['s1', 's2', 's3']

I am able to create a string with the content of the list. I'm also able to get the data I need based on this list.

str(ListA[0]) + '_' + str(ListB[0])   

However, I can't find a way to declare this as a variable name. Basically, this is the result that I want:

AA_s1 = stuff

If I try the following, it returns a string.

List[0] + '_' + ListB[0]
List[0] + ListB[0]

And if I try something simple like this, it just replaces the value (which is expected actually).

List[0] = stuff

Ultimately, I will create a function with for that will read all data frames that I need. That's why I want to control the variable names this way, instead of writing them down one by one. I believe that the key is to find the equivalent of str(data). I looked into the documentation and I cannot find anything similar to var, ToVar or something similar. Note that I do not have programming skills and I don't know any other language, which limits a bit my ability to research this.

Thanks

******Rev 01******

The thread is marked as a duplicate with this question. I went through the thread, and I found out that I can store the variables into a dictionary, I still cannot recall the data using the initial lists that I had.

Ultimately, my goal is to write:

ListA[i] + '_' + ListB[i] 
#where i is variable from 0 to ...
#Syntax above is most likely incorrect. 

and get the data frame stored into that. The name of the data frame is AA_s1, and it is accessible using:

dic['AA_s1']

However, I am still forced to write down manually the name of the variable that I am working with.

In an answer below, it is suggested that what I am trying to accomplish is impossible. If so, then please confirm.

1 Answers1

2

You can use a dictionary like so

ListA=['AA', 'BB', 'CC']
ListB=['s1', 's2', 's3']

dic = {}
dic[str(ListA[0]) + '_' + str(ListB[0])] = "stuff"
# dic = {'AA_s1': 'stuff'}

In this example, you now have a variable stored in dic named AA_s1 with value stuff

Hearner
  • 2,711
  • 3
  • 17
  • 34
  • Hello. I tried that, and it does work. However, I need to recall the dic["AA_s1"], which means that I still need to write it down manually. It does not allow me to recall directly the variable by typing: AA_s1. In addition, it does not allow me to recall this variable inside a loop/for. I want to be able ultimately to have a dataframe identified as AA_s1 and be able to recall that dataframe using the lists. Something like: ListA[0] + '_' + ListB[0] where the "0" would be replace by an iterative number such as "i". – Jean-François Beaubien Aug 07 '18 at 14:27
  • Why do you mean by `It does not allow me to recall directly the variable by typing: AA_s1` ? – Hearner Aug 07 '18 at 14:29
  • Let's say I write: A1=25. When I type A1, it will return 25. If I write AA_s1, it returns an error. I need to use dic['AA_s1']. – Jean-François Beaubien Aug 07 '18 at 14:31
  • Yes because the variable doesn't exist by itself. It's defined inside the dictionary, you have to call it using `dic['AA_s1']`. There is no other way to create dynamic variables in Python – Hearner Aug 07 '18 at 14:35
  • Is it possible to call that variable with the initial lists ? Something like dic['ListA[0] + '_' + ListB[0]'] Since the "0" will be controlled by an iterative, I want to be able to call it that way. I put some small lists, but let's say that each list has 15 elements, it means that I have 225 dataframe to deal with. To recall data into a specific dataframe, and do some analysis, let's say accross the list A, I would need at some point to write manually AA_s1, BB_s1, etc. – Jean-François Beaubien Aug 07 '18 at 14:42
  • 1
    Well you can iterate using the dictionary, that's the same as using the variable name but you just have to put the "string name of the variable" in the dictionary – Hearner Aug 07 '18 at 14:49