python cookbook   Recipe 2.5. Counting Lines in a File ,

今日发现一个新函数 enumerate 。一般情况下对一个列表或数组既要遍历索引又要遍历元素时,会这样写:

for i in range ( 0 , len ( list )):
    print i , list [ i ]
 
但是这种方法有些累赘,使用 内置enumerrate函数会有更加直接,优美的做法,先看看enumerate的定义:
def enumerate ( collection ):
    'Generates an indexed series:  (0,coll[0]), (1,coll[1]) ...'      
     i = 0
     it = iter ( collection )
     while 1 :
     yield ( i , it . next ())
     i += 1

enumerate会将数组或列表组成一个索引序列。使我们再获取索引和索引内容的时候更加方便如下:

for index , text in enumerate ( list )):
   print index , text

在 cookbook里介绍,如果你要计算文件的行数,可以这样写:

count = len ( open ( thefilepath , ‘ rU ’ ) . readlines ())

前面这种方法简单,但是可能比 较慢,当文件比较大时甚至不能工作,下面这种循环读取的方法更合适些。

Count = - 1
For count , line in enumerate ( open ( thefilepath , ‘ rU ’ )) :
    Pass
Count += 1

python --enumerate用法相关推荐

  1. python enumerate用法_【Python】python enumerate用法总结

    enumerate()说明 enumerate()是python的内置函数 enumerate在字典上是枚举.列举的意思 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enum ...

  2. python enumerate用法总结_python enumerate用法总结

    enumerate()说明 enumerate()是python的内置函数 enumerate在字典上是枚举.列举的意思 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enum ...

  3. python enumerate用法总结(转)

    原文链接:https://blog.csdn.net/churximi/article/details/51648388 enumerate()说明 enumerate()是python的内置函数 e ...

  4. python enumerate用法

    通常情况下我们在C++中遍历数组的方法为: for(int i=0; i<list.length; i++) {//deal with list[i] } python中我们可以这样遍历数组(字 ...

  5. python enumerate用法总结

    enumerate()说明 enumerate()是python的内置函数 enumerate在字典上是枚举.列举的意思 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enum ...

  6. python enumerate用法_Python enumerate 使用技巧

    enumerate() 是Python内建的函数,能让打印的结果更清晰,不管是列表,元组,字典,enumerate()都可以帮你完成,在某些需求下还是非常好用的. >>> a = [ ...

  7. python enumerate()用法

    enumerate enumerate()函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在for循环当中.Python2.3.以上版本可用,2 ...

  8. python enumerate_Python中enumerate用法详解

    enumerate()是python的内置函数.适用于python2.x和python3.x enumerate在字典上是枚举.列举的意思 enumerate参数为可遍历/可迭代的对象(如列表.字符串 ...

  9. python enumerate 行号 序号

    原文:http://blog.csdn.net/churximi/article/details/51648388 python enumerate用法总结 enumerate()说明 enumera ...

最新文章

  1. 机器人进攻民用市场:踏实做好小优美
  2. React从入门到精通系列之(1)安装React
  3. REST 架构是什么
  4. java reflectionutils_ReflectionUtils工具类-装载
  5. Go语言中使用MySql数据库
  6. C#中combobox不可编辑与不可选择
  7. 「管理数学基础」3.3 凸分析:凸函数的极值和凸规划
  8. IPC生产者与消费者模型加线程
  9. php aws_2018-03-06 使用AWS PHP SDK将文件上传到AMAZON S3
  10. 学好mysql数据库能找到什么工作_MySQL数据库学习路线汇总整理全集(附详细的学习路线介绍图)...
  11. java.security.NoSuchAlgorithmException: SHA256WithRSAandMGF1 Signature not available
  12. 解决cannot find grldr in all devices问题
  13. 转化二进制数的c语言程序,c语言实现二进制数转换为十进制
  14. 企业邮箱大全,企业邮箱查询,最大的邮箱是哪个?
  15. a到z的ascii码值是多少_大小写字母和数字的ascii码值是多少
  16. Centos7下为nvidia显卡安装驱动
  17. java集成微信发送模板消息
  18. 为什么说多项式时间复杂度的算法要优于指数时间复杂度的算法?
  19. 阿里云OSS上传文件设置请求头
  20. 计算机毕业设计 SSM+Vue停车位管理系统 临时停车位管理系统 车位停车管理系统 停车位租用管理系统Java Vue MySQL数据库 远程调试 代码讲解

热门文章

  1. Ended a touch event which was not counted in trackedTouchCount (ReactNative RN开发时多点触摸滑动报错)
  2. 【Python】字符串转换为ASCII码
  3. Android 编译之make基础(转)
  4. 浏览器端转盘抽奖策略实现
  5. css制作实心三角形
  6. 适合手机页面微信页面pc页面用的幻灯片
  7. C++ MFC日志Log类 棒棒的 控制台或文件或一起输出
  8. 2020年TI杯大学生电子设计大赛 无线运动传感器节点 备赛、参赛实录(历时一个月)
  9. SpringMVC学习----请求与响应
  10. 特种设备制造许可证多少钱