In Python, no. In Matlab, probably no. What your asking for doesn't even really make sense. How can you assign x
, a name, to 3
, an integer literal? You can't assign anything to an integer literal, anyway! And what do you mean by x? The string "x"
? The functionality you are probably looking for would be achieved by a map. Essentially, a map is a collection of key, value pairs where you access the value with the key. Keys must be unique, but the same value can have different keys. Python has a built-in data structure called a dict
for this very purpose:
num_to_letter = {1:'x', 2: 'y'}
print(num_to_letter[1])
print(num_to_letter[2])
You can choose whether to contain the reverse mapping in the same dict
or make another dict for that purpose.
letter_to_num = {}
for key in num_to_letter:
letter_to_num[num_to_letter[key]] = key
print(letter_to_num)
Once you get used to python, you can start using dictionary comprehensions in python 3:
letter_to_num = {v:k for k,v in num_to_letter.items()}
In python 2, you will have to use the following:
letter_to_num = dict((v,k) for k,v in num_to_letter.items())