# python迭代器索引 - 数组python

## 在'for'循环中访问索引? (14)

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

# 使用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)
``````

`0`以外的`1`开始它非常简单：

``````for index, item in enumerate(iterable, start=1):
print index, item
``````

## 注意

``````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)]
``````

``````for idx, val in enumerate(ints):
print(idx, val)
``````

``````alist = [ 1, 2, 3, 4, 5 ]

for n,a in enumerate(alist):
print( "%d %d"%(n,a) )
``````

``````0 1
1 2
2 3
3 4
4 5
``````

``````for n,a in enumerate(alist[1:-1]):
print( "%d %d"%(n,a) )
``````

``````0 2
1 3
2 4
``````

``````for n,a in enumerate(alist,start=1):
print( "%d %d"%(n,a) )
``````

``````1 1
2 2
3 3
4 4
5 5
``````

``````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]
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)]
``````

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

``````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 i in range(len(sequence)):
# work with index i
``````

``````for i in range(len(sequence)):
e = sequence[i]
# work with index i and element e
``````

``````for i, e in zip(range(len(sequence)), sequence):
# work with index i and element e
``````

``````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)
>>>
``````

``````In [1]: ints = [8, 23, 45, 12, 78]

In [2]: for idx, val in enumerate(ints):
...:         print(idx, val)
...:
(0, 8)
(1, 23)
(2, 45)
(3, 12)
(4, 78)
``````

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

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

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