如何串联对象以在Python中创建或调用变量? -编辑 [英] How to concatenate objects to create or call a variable in Python? - EDITED
问题描述
在我习惯的其他一些编程语言中,有一种方法可以动态创建和调用变量。
In some other programming languages I am used to, there was a way to dynamically create and call variables. How to do this in Python?
例如,假设变量名为test1; test2等,直到test9,我想这样称呼他们:
For example let's suppose variables called test1; test2 etc. up to test9, and I want to call them like this:
for n in range(1,10):
print concatenate('test', n)
当然,函数concatenate是我在找一个。
Of course, the function concatenate is the one I am looking for.
以这种方式组合字符串,整数和正则表达式的命令是什么?
What is the command to combine strings, integers and regular expressions in this way?
我的示例是一个不好的示例,使某些答案提示字典或其他类似方法。我可能对它们不太自信,或者不能在所有情况下都使用它们。这是另一个示例:
My example was a bad one, that made some of the answers suggest dictionary, or some other similar methods. It's either I am not too confident with them, or I can't use them in all cases. Here's another example:
假设我们有一个包含4行4列的表,并且该表中包含一些数字。我想做一些特殊的数学运算,将行号,列号和变量作为输入,并为另一个数学运算输出另一个行-列对。
Let's suppose we have a table of 4 rows and 4 columns, and the table has some numbers in it. I want to do some special mathematical operations, which has the row number, column number and the variable as inputs, and it outputs another row-column pair for another mathematical operation.
逻辑建议我,最简单的方法是拥有16个变量,并在其名称中包含行号和列号。而且,如果我可以使用变量名称进行操作,并返回结果并将其称为另一个varialbe,那么问题就很容易了。
Logic suggests me that te easiest way to do this would be to have 16 variables, having row and column number in their names. And if I could do operations with the names of the variables, and also return the result and call it as another varialbe, the problem would be easy.
这种情况呢? ?
推荐答案
您可以使用 globals
,它返回有关全局范围的字典:
You could use globals
, which returns a dictionary of what is in the global scope:
>>> test1 = 'a'
>>> test2 = 'b'
>>> test3 = 'c'
>>> globals()
{'test1': 'a', 'test3': 'c', 'test2': 'b', '__builtins__': <module '__builtin__' (built-in)>, '__package__': None, '__name__': '__main__', '__doc__': None}
>>> for n in range(1, 4):
... print globals()["test" + str(n)] # Dynamically access variables
...
a
b
c
>>> for n in range(1, 4):
... globals()["dynamic" + str(n)] = n # Dynamically create variables
...
>>> dynamic1
1
>>> dynamic2
2
>>> dynamic3
3
>>>
但是,我不建议您实际执行此操作。这是丑陋的,骇人听闻的,实际上只是一种不好的做法。
However, I do not recommend that you actually do this. It is ugly, hackish, and really just a bad practice.
最好删除数字变量,而使用< a href = https://docs.python.org/2/library/stdtypes.html#typesseq rel = nofollow>列表,该列表会自动按索引编号其项目:
It would be much better to do away with your numbered variables and instead use a list, which will automatically number its items by index:
>>> lst = ['a', 'b', 'c']
>>> lst[0] # Equivalent to test1
'a'
>>> lst[1] # Equivalent to test2
'b'
>>> lst[2] # Equivalent to test3
'c'
>>> for i in lst: # You can iterate directly over the list too
... print i
...
a
b
c
>>> lst.append('d') # "Creating" a new object is easy and clean
>>> lst
['a', 'b', 'c', 'd']
>>>
基本上,而不是 test1
, test2
, test3
...您现在拥有 lst [0]
, lst [1]
, lst [2]
...
Basically, instead of test1
, test2
, test3
...you now have lst[0]
, lst[1]
, lst[2]
...
这篇关于如何串联对象以在Python中创建或调用变量? -编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!