2

So I am working on a project and right now, I am at a point where I have a list of variables containing strings.

var1 = "one"
var2 = "two"
var3 = "three"
lst = [var1, var2, var3]

Basically what I want to do is get usr_input

usr_input = "one"

Then see if the list contains any variables with an equal string for value and print out the name of the variable.

With the above values the output would be:

var1
godot
  • 3,422
  • 6
  • 25
  • 42
JustV1b1n9
  • 33
  • 3
  • You're not going to be able to do this in a good way. If you need to match strings to values, you should put them in a dictionary. `{'var1': 'one', 'var2': 'two'}` ... etc – Mark Feb 28 '21 at 16:50
  • You won't be able to do this using just `lst`. The variable names have lost any association with the values in `lst`. – CryptoFool Feb 28 '21 at 16:51
  • Why do you need this? It would be easier to just have `lst = ["one", "two", "three"]`, then use `lst.index(usr_input)`. Or, if you really want to tie those values to some variable names (although I don't see the point), use a dictionary. – CristiFati Feb 28 '21 at 16:51
  • You pretty much never want to use a sequence of variable names like that. Rather, you want to use a dictionary or a list, as others have suggested. - the names of variables should never be important to the actual function of your program. – CryptoFool Feb 28 '21 at 16:52
  • [similar question](https://stackoverflow.com/questions/592746/how-can-you-print-a-variable-name-in-python) – Aven Desta Feb 28 '21 at 17:05

3 Answers3

0

I suggest you to use dictionary for your problem. First store your values into dictionary:

d = { 'var1': 'one', 'var2': 'two', 'var3': 'three' }

and then search the key in the dictionary by value:

usr_input = 'one'
for k,v in d.items():
        if v == usr_input:
                print(k)
                break
godot
  • 3,422
  • 6
  • 25
  • 42
  • That indeed does work if all I wanted was the name of the variable. Problem is the name of the variable is used to tell the program in which EXISTING variable to assign a specific value.. I totally failed to explain this in my question and I am really sorry to have waisted your time.. I've posted another question which is a bit more specific.. Maybe you can understand a bit more with that! Here's the link: https://stackoverflow.com/questions/66442415/python-get-user-input-and-loop-through-a-lot-of-variables-in-order-to-match-tha – JustV1b1n9 Mar 02 '21 at 15:51
0

To most easily get the behavior you are asking for, you should create a map that maps variable values to variable names, like this:

var1= "one"
var2= "two"
var3= "three"
d = { var1: 'var1', var2: 'var2', var3: 'var3' }

user_input = 'one'

print (d[user_input])

Result:

var1

As you can see, the link back to the original variable name is artificial. There's no way to get back to the actual variable name once you've used the variable name elsewhere to set some other variable or data structure entity.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • I understand my question wasn't specific at all and as I mentioned to the other people who answered.. Thanks for taking the time to answer and I'm sorry to have waisted that time of yours.. I've put up the same question but a lot more specific so maybe you can understand better what I am trying to achieve! Here's the link: https://stackoverflow.com/questions/66442415/python-get-user-input-and-loop-through-a-lot-of-variables-in-order-to-match-tha – JustV1b1n9 Mar 02 '21 at 15:48
0
# var1= "one"
# var2= "two"
# var3= "three"
# lst = [var1, var2, var3]

# What you should alway strive to do is to define your initial data in a way that is
# as simple and yet as usable as possible.  Here, you want to assign words to numeric
# values, and you also want to be able to go in the other direction, mapping words to
# numeric values (possibly in the form of `var1`, `var1`, etc.  Actually defining the
# variables `var1`, `var2`, etc. can serve no purpose over defining a list containing
# the same values as these variables. Using a list means that your code isn't tied to
# the number of items you are processing, and it lets you iterate over the values in
# any way that you want to.  If you only use `var1`, `var2`, etc. to set the initial
# values of a list, why not just define the constant values right in the list.

# Here's the most concise way to define your incoming data.  It defines each word along
# with a numeric value, the position in the list at which each word is found.  This
# will let you do everything you want to do with your data, with the simplest initial
# definition of that data.

data = [ 'one', 'two', 'three']

# You will want to be able to map words back to numeric values, the positions in the
# original 'data' list.  To do that, you create a map mapping words to the index
# positions of each word in the original list.

index = { k: i for i, k in enumerate(data) }

# Now it is trivial to map a word back to an index in the original list:

user_input = 'one'
i = index[user_input]

# Your requirements say that you want to print "varN" where N is the Nth
# variable in your code.  If you really care about those names, you can
# reconstruct and print the appropriate one as follows:

name = "var" + str(index[user_input] + 1)
print (name)

# This prints the result you desire:
#
#    var1

# But why do you need that name?  Do you rather just want to map the word back
# to the original storage location with which it was associated.  If that's what
# you want, then that's the "i" you just computed above.  The storage location
# itself is "data[i]".  In a real program, one of these two values, "i" or "data[i]"
# is probably what you're really interested in.  The need to introduce an
# arbitrary number of variables in your code, `var1`, `var2`, `var3`. etc. is
# pretty much never what you want.

# A simpler way to look at this is to think about what it would take in your original
# code to support, say, 9 values.  You'd have a lot of work to do to add those values,
# and you wouldn't yet be able to get the behavior you want from the code.  With the
# solution presented here, you change just one line of code, as follows:

data = [ 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']

# and now your code supports the user typing any of these values.  you could trivially
# read these values from a file, and then your code wouldn't need to change the code at
# all. That should always be your goal...to keep input data separate from functional code.

# To be able to help you further, we need to know more about what you're trying to
# do here, or what you're trying to represent in a larger program.  Tell us more,
# and we can give you more direction.
Rapt0r
  • 79
  • 3
  • Thanks for all the time you put to write that answer ! I am sorry I understand I wasn't specific at all and that did not help. I've posted another question which is about the same exact thing but 'better phrased' I think.. Maybe you could understand a bit more trough that. Here's the link: https://stackoverflow.com/questions/66442415/python-get-user-input-and-loop-through-a-lot-of-variables-in-order-to-match-tha – JustV1b1n9 Mar 02 '21 at 15:40