Python 使用循环为列表中的变量赋值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4687364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assigning values to variables in a list using a loop
提问by johnharris85
var_list = [one, two, three]
num = 1
for var in var_list:
var = num
num += 1
The above gives me an error that 'one' doesn't exist. Can you not assign in this way? I want to assign an incrementing number for each var in the list.
以上给了我一个错误,即“一个”不存在。不能这样赋值吗?我想为列表中的每个 var 分配一个递增的数字。
采纳答案by Karl Knechtel
Python variables are names forvalues. They don't really "contain" the values.
Python 变量是值的名称。它们并不真正“包含”这些值。
for var in var_list:causes 'var' to become a name for each element of the list, in turn. Inside the loop, var = numdoes not affect the list: instead, it causes varto cease to be a name for the list element, and instead start being a name for whatever numcurrently names.
for var in var_list:依次导致“var”成为列表中每个元素的名称。在循环内部,var = num不会影响列表:相反,它导致var不再是列表元素的名称,而是开始成为任何num当前名称的名称。
Similarly, when you create the list, if one, twoand threearen't already names for values, then you can't use them like that, because you are asking to create a list of the values that have those names (and then cause var_listto be a name for that list). Note that the list doesn't really contain the values, either: it references, i.e. shares them.
同样,当你创建列表,如果one,two而three不是已经为值的名字,那么你可以不使用它们一样,因为你问到创建具有这些名称(进而导致值的列表var_list是该列表的名称)。请注意,该列表也并不真正包含值:它引用,即共享它们。
回答by TryPyPy
No, it doesn't work like that.
不,它不是那样工作的。
You can try:
你可以试试:
one, two, three = range(1, 4)
This work by defining the variables in a multiple assignment. Just as you can use a, b = 1, 2. This will unroll the rangeand assign its values to the LHS variables, so it looks like your loop (except that it works).
这是通过在多重赋值中定义变量来实现的。正如您可以使用a, b = 1, 2. 这将展开range并将其值分配给 LHS 变量,因此它看起来像您的循环(除了它可以工作)。
Another option (which I wouldn't recommend in a real program...) would be to introduce the variable names in an exec statement:
另一种选择(我不会在实际程序中推荐......)是在 exec 语句中引入变量名称:
names = ['one', 'two', 'three']
num = 1
for name in names:
exec "%s = %s" % (name, num)
num += 1
print one, two, three
回答by Bryan Oakley
A variable doesn't exist until you create it. Simply referencing a variable isn't enough to create it. When you do [one, two, three]you are referencing the variables one, twoand threebefore they are created.
变量在创建之前不存在。仅仅引用一个变量不足以创建它。当你这样做时,[one, two, three]你正在引用变量one,two并且three在它们被创建之前。
回答by Michael Cornelius
You can access the dictionary of global variables with the globals()built-in function. The dictionary uses strings for keys, which means, you can create variables with names given as strings at run-time, like this:
您可以使用globals()内置函数访问全局变量字典。字典使用字符串作为键,这意味着您可以在运行时创建名称为字符串的变量,如下所示:
>>> var_names = ["one", "two", "three"]
>>> count = 1
>>> for name in var_names:
... globals()[name] = count
... count += 1
...
>>> one
1
>>> two
2
>>> three
3
>>> globals()[raw_input()] = 42
answer
>>> answer
42
回答by Prometheus
I think this approach is simpler.
我认为这种方法更简单。
var_list = [1, 2, 3]
num = 1
for index,var in enumerate(var_list):
var_list[index] = num
num += 1

