无法在循环 Python 中修改列表元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19290762/
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
Can't modify list elements in a loop Python
提问by user2717129
While looping over a list in Python, I was unable to modify the elements without a list comprehension. For reference:
在 Python 中循环遍历列表时,我无法在没有列表理解的情况下修改元素。以供参考:
li = ["spam", "eggs"]
for i in li:
i = "foo"
li
["spam", "eggs"]
li = ["foo" for i in li]
li
["foo", "foo"]
So, why can't I modify elements through a loop in Python? There's definitely something I'm missing, but I don't know what. I'm sure this is a duplicate, but I couldn't find a question about this, and if there is a link, that would be more than enough. Thank you in advance!
那么,为什么我不能在 Python 中通过循环修改元素呢?肯定有一些我想念的东西,但我不知道是什么。我确定这是重复的,但我找不到关于此的问题,如果有链接,那就足够了。先感谢您!
采纳答案by justhalf
Because the way for i in li
works is something like this:
因为for i in li
工作方式是这样的:
for idx in range(len(li)):
i = li[idx]
i = 'foo'
So if you assign anything to i
, it won't affect li[idx]
.
因此,如果您将任何内容分配给i
,它不会影响li[idx]
。
The solution is either what you have proposed, or looping through the indices:
解决方案要么是您提出的,要么是遍历索引:
for idx in range(len(li)):
li[idx] = 'foo'
or use enumerate
:
或使用enumerate
:
for idx, item in enumerate(li):
li[idx] = 'foo'
回答by jabaldonedo
In fact with list comprehension you are not modifying the list, you are creating a new list and then assigning it to the variable that contained the previous one.
事实上,对于列表理解,您不是在修改列表,而是在创建一个新列表,然后将其分配给包含前一个列表的变量。
Anyway, when you do for i in li
you are getting a copy of each value of li
in variable i
, you don't get the reference to a position in li
, so you are not modifying any value in li
.
无论如何,当您for i in li
获得li
in variable的每个值的副本时i
,您不会获得对 in 中位置的引用li
,因此您不会修改 中的任何值li
。
If you want to modify your list you can do it with enumerate
:
如果你想修改你的列表,你可以这样做enumerate
:
>>> li = ["spam", "eggs"]
>>> for i,_ in enumerate(li):
li[i] = "foo"
>>> li
['foo', 'foo']
or with xrange
(in Python 2.7, use range in python 3):
或使用xrange
(在 Python 2.7 中,在 Python 3 中使用范围):
>>> for i in xrange(len(li)):
li[i] = "foo"
>>> li
['foo', 'foo']
or with the list comprehension you showed in your question.
或使用您在问题中显示的列表理解。
回答by user278064
I'm able to modify a list while looping:
我可以在循环时修改列表:
lst = range(10) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for i, elem in enumerate(lst):
lst[i] = 0 // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
回答by alandarev
for element in li
- returns you a copy of element, not the element itself.
for element in li
- 返回元素的副本,而不是元素本身。
Solution for your case would be:
您的情况的解决方案是:
for i in range(len(li)):
li[i] = 'foo'
回答by grasshopper
Maybe using dictionaries might be helpful.
也许使用字典可能会有所帮助。
>>> li = {0: "spam", 1:"eggs"}
for k, v in li.iteritems():
li[k] = "foo"
>>> li
{0: 'foo', 1: 'foo'}