# 使用enumerate()实现 ints = [8, 23, 45, 12, 78] for idx, val in enumerate(ints): print(idx, val) ints = [8, 23, 45, 12, 78] for index, item in enumerate(ints, start=0): # 默认是从 0 开始 print(index, item) ints = [8, 23, 45, 12, 78] for index, item in enumerate(ints, start=1): # 索引修成从 1 开始 print(index, item)
# 使用range()实现 ints = [8, 23, 45, 12, 78] for i in range(len(ints)): print(i, ints[i])
# 仅使用for和in实现 ints = [8, 23, 45, 12, 78] index = 0 for value in ints: index +=1 print(index, value) ints = [8, 23, 45, 12, 78] index = 0 for value in (ints): index +=1 print index, value if index >= len(ints)-1: # 重置index索引 index = 0