0

I have a large amount of data in variable form:

abc_123 = 5  
def_456 = 7  
ghi_789 = 9 

etc.

I ask the user for various inputs, which builds up a string. For example:

temp = "abc_123"

How do I make it so that temp = abc_123, thus making temp = 5?

1 Answers1

1

Make a dictionary and then look up the values by key:

>>> d = {"abc_123": 5, "def_456": 7, "ghi_789": 9}
>>> temp = "abc_123"
>>> d[temp]
5
>>> d.get("invalid", "not found")
'not found'
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks! That worked great. It took some time to rearrange all the data in that format, but it wasn't excruciating enough to write code just to do that. – remote_geologist Apr 05 '16 at 18:43