标签:列表 -- string 工作 pen ble 序列 测试 方法
python之枚举
内置函数 enumearate()
list=["这","是","一个","测试"] for i in range(len(list)): print(i,list[i])
list=["这","是","一个","测试"] for index,iterm in enumerate(list): print(index,iterm)
list=["这","是","一个","测试"] for index,iterm in enumerate(list,1): print(index,iterm)
如果要统计文件的行数,可以这样写:
count = len(open(filepath, ‘r‘).readlines())
这种方法简单,但是可能比较慢,当文件比较大时甚至不能工作。
可以利用enumerate():
count = 0
for index, line in enumerate(open(filepath,‘r‘)):
count += 1
标签:列表 -- string 工作 pen ble 序列 测试 方法
原文地址:https://www.cnblogs.com/zhuhaofeng/p/9570557.html