for使用循环迭代序列时如何访问索引?
xs = [8, 23, 45]
for x in xs:
print("item #{} = {}".format(index, x))
期望的结果:
item #1 = 8
item #2 = 23
item #3 = 45
来源:https://stackoverflow.com/questions/522563/how-to-access-the-index-value-in-a-for-loop
for使用循环迭代序列时如何访问索引?
xs = [8, 23, 45]
for x in xs:
print("item #{} = {}".format(index, x))
期望的结果:
item #1 = 8
item #2 = 23
item #3 = 45
来源:https://stackoverflow.com/questions/522563/how-to-access-the-index-value-in-a-for-loop
使用索引:
由于列表元素可以通过索引访问,因此我们将遍历所有索引并将相应的元素配对。我们还要记住,索引从 到 编号
0,因此我们在输出时длины списка - 1将增加索引值。1使用枚举函数:
我们将迭代元素,该函数
enumerate还将为我们支持计数器。我们将列表本身作为第一个参数传递enumerate,第二个参数是我们要开始计数的值,即1。