# 使用for循环，如何访问循环索引，在这种情况下从1到5？

``````for index, item in enumerate(items):
print(index, item)
``````

``````for count, item in enumerate(items, start=1):
print(count, item)
``````

# Unidiomatic控制流程

``````index = 0            # Python's indexing starts at zero
for item in items:   # Python's for loops are a "for each" loop
print(index, item)
index += 1
``````

``````index = 0
while index < len(items):
print(index, items[index])
index += 1
``````

``````for index in range(len(items)):
print(index, items[index])
``````

# 使用枚举函数

Python的`enumerate`函数通过隐藏索引的会计，并将iterable封装到另一个迭代（ `enumerate`对象）中来减少视觉混乱，该迭代产生索引的两项元组和原始迭代将提供的项。 看起来像这样：

``````for index, item in enumerate(items, start=0):   # default is zero
print(index, item)
``````

## 算一算

``````for count, item in enumerate(items, start=1):   # default is zero
print(item)

print('there were {0} items printed'.format(count))
``````

## 打破它 - 一步一步的解释

``````items = ['a', 'b', 'c', 'd', 'e']
``````

``````enumerate_object = enumerate(items) # the enumerate object
``````

``````iteration = next(enumerate_object) # first iteration from enumerate
print(iteration)
``````

``````(0, 'a')
``````

``````index, item = iteration
#   0,  'a' = (0, 'a') # essentially this.
``````

``````>>> print(index)
0
>>> print(item)
a
``````

# 结论

• Python索引从零开始
• 要在迭代时从迭代中获取这些索引，请使用枚举函数
• 以惯用的方式使用枚举（以及元组解包）创建更易读和可维护的代码：

``````for index, item in enumerate(items, start=0):   # Python indexes start at zero
print(index, item)
``````
Question

## python打印list

``````ints = [8, 23, 45, 12, 78]
``````

``````for i in range(len(ints)):
print i, ints[i]
``````

``````for ix in range(len(ints)):
print ints[ix]
``````

``````[ (ix, ints[ix]) for ix in range(len(ints))]

>>> ints
[1, 2, 3, 4, 5]
>>> for ix in range(len(ints)): print ints[ix]
...
1
2
3
4
5
>>> [ (ix, ints[ix]) for ix in range(len(ints))]
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
>>> lc = [ (ix, ints[ix]) for ix in range(len(ints))]
>>> for tup in lc:
...     print tup
...
(0, 1)
(1, 2)
(2, 3)
(3, 4)
(4, 5)
>>>
``````

``````int_list = [8, 23, 45, 12, 78]
for index, value in enumerate(int_list):
print(index, value)
``````

``````0 8
1 23
2 45
3 12
4 78
``````

``````for x in range(0, 5):
``````

``````list[index]
``````

``````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 = 0
``````

``````data = ['itemA.ABC', 'itemB.defg', 'itemC.drug', 'itemD.ashok']
x = []
for (i, item) in enumerate(data):
a = (i, str(item).split('.'))
x.append(a)
for index, value in x:
print(index, value)
``````

``````0 ['itemA', 'ABC']
1 ['itemB', 'defg']
2 ['itemC', 'drug']
3 ['itemD', 'ashok']
``````

``````ints = [8, 23, 45, 12, 78]
inds = [ints.index(i) for i in ints]
``````

``````ints = [8, 8, 8, 23, 45, 12, 78]
inds = [tup[0] for tup in enumerate(ints)]
``````

``````ints = [8, 8, 8, 23, 45, 12, 78]
inds = [tup for tup in enumerate(ints)]
``````

``````for i in ints:
indx=ints.index(i)
print(i,indx)
``````

``````for counter, value in enumerate(ints):
print(counter, value)
``````

``````for counter in range(len(ints)):
print(counter,ints[counter])
``````

``````ints = [8, 23, 45, 12, 78]
print [(i,ints[i]) for i in range(len(ints))]
``````

``````[(0, 8), (1, 23), (2, 45), (3, 12), (4, 78)]
``````

### Tags

python   loops   list