Because the way for i in li
works is something like this:
for idx in range(len(li)):
i = li[idx]
i = 'foo'
So if you assign anything to i
, it won't affect 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
:
for idx, item in enumerate(li):
li[idx] = 'foo'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…