4

I have a set of table names

1. EOM
2.STMT
3.LOOKUP etc

I want to associate these table names with some variables names such as

1. start_time, 
2. end_time, 
3. total_time etc. 

The way I want to write these variable names is something like

1. start_time_EOM, end_time_EOM, total_time_EOM
2. start_time_STMT, end_time_STMT,total_time_STMT
3. start_time_LOOKUP,end_time_LOOKUP,total_time_LOOKUP

Can this be done in python, and how? (Note: I am new to Python, and still trying to learn).

3
  • 1
    use a dictionary Commented Jul 7, 2017 at 15:48
  • Are you using a library like Pandas or something? You mentioned tables... Commented Jul 7, 2017 at 15:49
  • 1
    Yes, it can be done by polluting globals() and no, you should not be doing it. Commented Jul 7, 2017 at 15:50

2 Answers 2

3

Yup, dictionaries are solution:

tables=["A","B","C"]
vars = ["var1","var2","var3"]
dict={}
for val in tables:
  for val2 in vars:
    dict[val2+"_"+val] = foo
    dict[val2+"_"+val] = bar
    dict[val2+"_"+val] = foobar
#and so 

Hope if helps

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! This was very helpful. Question: How can the same problem be solved in R?
@Gompu Sorry, IDK about R. Hope you find the solution. Greetings!
2

You cannot change variables names the way you are looking to do, but you might try using a python dictionary to achieve what you're trying to accomplish.

2 Comments

you could probably jury-rig something using eval() but Cary is right, you really don't want to do it this way.
Thank you so much! How can the same problem be solved in R?

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.