2017-08-30 134 views
1
>>a=input("Enter Variable Value: ") 
123abc321zxc 
>>b=input("Enter Variable Name: ") 
xyz 

現在我想XYZ應被認爲是變量名和123abc321zxc作爲它的值,這樣我可以(的「a」到被分配到B的值的值)得到如下結果:分配值給變量名在Python

XYZ = 「123abc321zxc」

注意:不允許使用數據庫。它不是任何問題的重複。請不要大關它複製

請建議

+2

[在蟒蛇飛行產生的變量名]的可能的複製(https://stackoverflow.com/questions/4010840/generating-variable-names-on-fly-in-python) –

+0

沒有。它不重複。 –

回答

1

要創建具有動態名稱的變量,你可以這樣做:

globals()['var_name'] = 'var_value' 

這爲我工作:

>>> globals()[input("Enter Variable Name: ")] = input("Enter Variable Value: ") 
Enter Variable Value: 123 
Enter Variable Name: aabbcc 
>>> print(aabbcc) 
123 
>>> 

編輯

讓我知道這是否是細跟你:

>>> g = globals() # Get the *current global symbol table*. 
>>> var_name = input("Enter Variable Name: ") 
Enter Variable Name: aabbcc 
>>> var_value= input("Enter Variable Value: ") 
Enter Variable Value: 123 
>> g[var_name] = var_value # Add to the *current global symbol table* a name (named as the value of the variable *var_name*, 'aabbcc' in this case) and value with the value of the variable *var_value*. 
>>> print(aabbcc) 
123 
>>> 
+0

對不起。但在這裏它只是a = b。它沒有達到我的目的。它就像我從UI創建變量名稱爲'xyz',並且在某些操作之後我獲得了該值。現在我想要將該值存儲在xyz變量名稱(從UI中給出) –

+0

@AmitKumar看看現在是否更清晰;) –

+0

感謝gsi-frank。這幫助了我。 :) –