Python 3.x for循环和列表索引

Gok*_*lai 3 python for-loop python-3.x

我无法理解,这个简单的问题在Python中使用for循环,我在在线测验中找到了.能帮我理解一下,为什么我们得到以下输出?

some_list = [1,2,3,4,5]

for some_list[1] in some_list:
    print(some_list)
    print(some_list[1])
Run Code Online (Sandbox Code Playgroud)

输出:

[1, 1, 3, 4, 5]
1
[1, 1, 3, 4, 5]
1
[1, 3, 3, 4, 5]
3
[1, 4, 3, 4, 5]
4
[1, 5, 3, 4, 5]
5
Run Code Online (Sandbox Code Playgroud)

我想,它将打印列表的第二个元素和整个列表5次.

Bil*_*ard 5

表达式for x in some_list:循环遍历列表并临时存储列表的每个值x.

表达式for some_list[1] in some_list:循环遍历列表并临时存储列表的每个值some_list[1].(循环的每次迭代,列表的下一个值都会覆盖some_list[1].)

我想,它将打印列表的第二个元素和整个列表5次.

这就是发生的事情,只是列表正在改变(特别是第二个元素).