【发布时间】:2017-12-31 18:04:35
【问题描述】:
下面是代码。为什么会这样?有什么替代方法?
line2 = ["a", "b","c","d","e","f","g","a","c", "d","e","g","h",]
for x in line2:
print("X value is:", x, " and its index is:", line2.index(x))
输出是:
X value is: a and its index is: 0
X value is: b and its index is: 1
X value is: c and its index is: 2
X value is: d and its index is: 3
X value is: e and its index is: 4
X value is: f and its index is: 5
X value is: g and its index is: 6
X value is: a and its index is: 0
X value is: c and its index is: 2
X value is: d and its index is: 3
X value is: e and its index is: 4
X value is: g and its index is: 6
X value is: h and its index is: 12
为什么它不增加?我该如何解决。我需要增加索引吗?而不是索引,我们有类似 X 的位置
【问题讨论】:
-
它怎么知道你问的是哪个
"c"?它给了你第一个。 -
每次迭代时 X 值是否递增,{ for x in line2} 部分?
-
X是列表的元素。没有什么可以增加的。