这怎么可能?
list = [0, 0, 0, 0]
for i in list:
print list.index(i),
list[ list.index(i) ] += 1
Run Code Online (Sandbox Code Playgroud)
这打印:0 1 2 3(这是可以理解的)
list = [1, 4, 5, 2]
for i in list:
print list.index(i),
list[ list.index(i) ] += 1
Run Code Online (Sandbox Code Playgroud)
打印:0 1 1 0
list.index(i)返回i列表中找到值的第一个索引.
> i = 1
prints: 0
list[0] = 1+1 = 2 -> list = [2,4,5,2]
> i = 4
prints: 1
list[1] = 4+1 = 5 -> list = [2,5,5,2]
> i = 5
prints: 1 #because that's where 5 is first found in the list
list[1] = 5+1 = 6 -> list = [2,6,5,2]
> i = 2
prints: 0 #because that's where 2 is first found in the list
list[0] = 2+1 = 3 -> list = [3,6,5,2]
Run Code Online (Sandbox Code Playgroud)