python函数之enumerate()

enumrate 语法:
# enumerate(sequence, [start=0])

参数:
# sequence -- 一个序列、迭代器或其他支持迭代对象。
# start -- 下标起始位置。



使用 enumrate 输出列表元素和序号(序号从 100 开始)
li =['eric','alex','tony']
for i,ele in enumerate(li,1):
    print(i,ele)
# 执行结果:
# 1 eric
# 2 alex
# 3 tony

 

enumerate 统计文件的行数
filepath = '/var/log/cron'
count = 0
for index, line in enumerate(open(filepath,'r')):
    count += 1
print(count)

 

posted @ 2018-12-29 17:05  LiShiChao  阅读(676)  评论(0)    收藏  举报