将字符串转换为变量名[duplicate]

时间:2022-10-15 16:22:31

This question already has an answer here:

这个问题已经有了答案:

I have any string. like 'buffalo',

我有任何字符串。像“水牛”,

x='buffalo'

I want to convert this string to some variable name like,

我想把这个字符串转换成变量名,

buffalo=4 

not only this example, I want to convert any input string to some variable name. How should I do that (in python)?

不仅是这个例子,我还想将任何输入字符串转换为某个变量名。我应该怎么做(用python)?

3 个解决方案

#1


56  

x='buffalo'    
exec("%s = %d" % (x,2))

After that you can check it by:

之后你可以通过:

print buffalo

As an output you will see: 2

作为输出,您将看到:2

#2


12  

This is the best way, I know of to create dynamic variables in python.

这是最好的方法,我知道在python中创建动态变量。

my_dict = {}
x = "Buffalo"
my_dict[x] = 4

I found a similar, but not the same question here Creating dynamically named variables from user input

我发现了一个类似的问题,但不是同样的问题:从用户输入中创建动态命名的变量

[Editted according to Martijn's suggestion]

[根据Martijn的建议编辑]

#3


10  

You can use a Dictionary to keep track of the keys and values.

您可以使用字典来跟踪键和值。

For instance...

例如……

dictOfStuff = {} ##Make a Dictionary

x = "Buffalo" ##OR it can equal the input of something, up to you.

dictOfStuff[x] = 4 ##Get the dict spot that has the same key ("name") as what X is equal to. In this case "Buffalo". and set it to 4. Or you can set it to  what ever you like

print(dictOfStuff[x]) ##print out the value of the spot in the dict that same key ("name") as the dictionary.

A dictionary is very similar to a real life dictionary. You have a word and you have a definition. You can look up the word and get the definition. So in this case, you have the word "Buffalo" and it's definition is 4. It can work with any other word and definition. Just make sure you put them into the dictionary first.

字典和现实生活中的字典很相似。你有一个词,你有一个定义。你可以查一查这个词并得到它的定义。在这个例子中,你有“野牛”这个词,它的定义是4。它可以与任何其他的词和定义一起工作。一定要先把它们放进字典。

#1


56  

x='buffalo'    
exec("%s = %d" % (x,2))

After that you can check it by:

之后你可以通过:

print buffalo

As an output you will see: 2

作为输出,您将看到:2

#2


12  

This is the best way, I know of to create dynamic variables in python.

这是最好的方法,我知道在python中创建动态变量。

my_dict = {}
x = "Buffalo"
my_dict[x] = 4

I found a similar, but not the same question here Creating dynamically named variables from user input

我发现了一个类似的问题,但不是同样的问题:从用户输入中创建动态命名的变量

[Editted according to Martijn's suggestion]

[根据Martijn的建议编辑]

#3


10  

You can use a Dictionary to keep track of the keys and values.

您可以使用字典来跟踪键和值。

For instance...

例如……

dictOfStuff = {} ##Make a Dictionary

x = "Buffalo" ##OR it can equal the input of something, up to you.

dictOfStuff[x] = 4 ##Get the dict spot that has the same key ("name") as what X is equal to. In this case "Buffalo". and set it to 4. Or you can set it to  what ever you like

print(dictOfStuff[x]) ##print out the value of the spot in the dict that same key ("name") as the dictionary.

A dictionary is very similar to a real life dictionary. You have a word and you have a definition. You can look up the word and get the definition. So in this case, you have the word "Buffalo" and it's definition is 4. It can work with any other word and definition. Just make sure you put them into the dictionary first.

字典和现实生活中的字典很相似。你有一个词,你有一个定义。你可以查一查这个词并得到它的定义。在这个例子中,你有“野牛”这个词,它的定义是4。它可以与任何其他的词和定义一起工作。一定要先把它们放进字典。