-2

I'm trying to build a function that automatically calls previously defined variables using a portion of the name of these previously defined variables.

For example:

List=('USGS46', 'USGS47', 'USGS48', 'USGS50')

USGS46_d17O= -15.6579
USGS46_d18O= -29.4758

USGS47_d17O= -10.48288938
USGS47_d18O= -19.8374292

USGS48_d17O= -1.144217023
USGS48_d18O= -2.210739818

USGS50_d17O= 2.583133734
USGS50_d18O= 4.920460156

for x in List:
    print(print(f"{''+x+'_d17O'}"))

I figured that print within print would first provide the variable name and then call it but instead I just get a 'None' returned for each.

USGS46_d17O 
None 
USGS47_d17O 
None 
USGS48_d17O 
None 
USGS50_d17O 
None

How can I get this to actually provide the values associated with each?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
lordseal92
  • 23
  • 6
  • 1
    Also, you mixed up the different ways to insert a variable into a string. Use either `x + '_d17O'` or `f'{x}_d17O'`. – mkrieger1 Apr 11 '23 at 19:46
  • Hi all, thank you for the quick responses! Yes; the following posts mostly address the issue I'm having. I just had to switch from 'print(print(f"{''+x+'_d17O'}"))' to 'print(eval(f"{''+x+'_d17O'}"))' and it worked! As mkrieger1 pointed out, I have now simplified this to 'print(eval(x+'_d17O'))'. I really appreciate the help! – lordseal92 Apr 11 '23 at 19:52
  • 1
    Use a dict, not individual variables! – deceze Apr 11 '23 at 19:53
  • 1
    Why did you choose `eval()` and not `globals()` or `getattr()`? Their respective answers are higher-voted. And they don't have a warning below them printed in bold text. – mkrieger1 Apr 11 '23 at 19:53
  • @deceze this is for a dict but I just wanted to give a brief example of the issue – lordseal92 Apr 11 '23 at 19:54
  • @mkrieger1 seemed like the most straightforward/simple solution for me but I'm fairly new with Python – lordseal92 Apr 11 '23 at 19:55
  • 1
    This is what you want: https://stackoverflow.com/a/1373185/476! Do not get into nonsensical habits with variable variables. – deceze Apr 11 '23 at 19:57
  • @deceze I hear you. Like I said, I was using dict in the actual code I just gave a brief example here to save time. Thanks again for the help. – lordseal92 Apr 11 '23 at 20:53

1 Answers1

0

The print function's return value is None. When you print(print(...)) it will first call the inner print function, which will just print the actual string each time, and this inner print will return a None value, which will later be printed.

To print the value of a variable given a string, you could use the function eval() so it would end up being:

List=('USGS46', 'USGS47', 'USGS48', 'USGS50')

USGS46_d17O= -15.6579
USGS46_d18O= -29.4758

USGS47_d17O= -10.48288938
USGS47_d18O= -19.8374292

USGS48_d17O= -1.144217023
USGS48_d18O= -2.210739818

USGS50_d17O= 2.583133734
USGS50_d18O= 4.920460156

for x in List:
    print(eval(x+"_d17O"))
Nicolas Tabet
  • 139
  • 1
  • 5
  • 1
    Thank you! Some of the earlier comments helped me find this answer but still really appreciate the help! – lordseal92 Apr 11 '23 at 19:56
  • What would be the equivalent to this solution in R? I'm once again getting stuck in the same spot using (eval((paste0(x,"_d18"))) in R? – lordseal92 May 04 '23 at 22:18