enumerate函数

enumerate(iterable, start=0)

  __next__()方法

# 枚举可迭代对象a
a = ['Mary', 'had', 'little']
for i, e in enumerate(a):
    print(i, e)
# 列出枚举对象
seasons = ['Spring', 'Summer', 'Fall', 'Winter']
print(list(enumerate(seasons)))
# 等价函数
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1

更多:

  Loop Methods 循环技巧

posted @ 2019-01-31 16:33  怒放吧!  阅读(240)  评论(0编辑  收藏  举报
我的