0

I'd like to iterate a python script over a set of data variables. I have input data variables named data_X, data_Y, etc. which are defined at the beginning of my script, e.g. data_X = [1, 2, 5, 42, 793]. In bash, I could just use "$X" to specify that I want the string, so I'm looking for a python equivelent. When I try

for variable in ['X', 'Y']:
run_data = 'data_' + variable 

what happens is that run_data is now equal to a string data_X, but not equal to the actual data that I had earlier defined as data_X

How do I specify that I want run_data to equal the values originally assigned to data_X?

Alison
  • 1
  • 2
  • ideally something like the equivalent of bash "$X" – Alison Aug 13 '18 at 11:25
  • Check the duplicate flag at the top of the page. – ech0 Aug 13 '18 at 11:25
  • edited to clarify – Alison Aug 13 '18 at 11:40
  • In `for variable in ['X', 'Y']:` you are accessing the variables X and Y. You do not have variables named `X` or `Y`. You have variables named `data_X` and `data_Y`. Have you tried using the variables names? – ech0 Aug 13 '18 at 11:41
  • Later in the script I need to call just 'X' and 'Y' for output, hence not wanting to have a bunch of unnecessary text in the names – Alison Aug 13 '18 at 13:57
  • Well if you want to access a variable you have to call it by its name. For example if I create a string called `helloWorld` and assign it as `Hello World! :)` and then I try saying `print(hello)` it will not work cause it is not assigned. In this case, you're looping through the list that contains values X and Y, which makes both your data_X and data_Y variables irrelevant, and you might as well remove them if this is what you wish – ech0 Aug 14 '18 at 06:10

0 Answers0