enumerate函数接受一个可遍历的对象,如列表、字符串,可同时遍历下标(index)及元素值(value)

  1. >>> a = ['aaa','bbb','ccc',1235]
  2. >>> print(a)
  3. ['aaa', 'bbb', 'ccc', 1235]
  4. >>> print(len(a))
  5. 4
  6. >>> for i in range(len(a)):
  7. print(i)
  8.  
  9. 0
  10. 1
  11. 2
  12. 3
  13. >>> for j in range(len(a)):
  14. print(j,a[j])
  15.  
  16. 0 aaa
  17. 1 bbb
  18. 2 ccc
  19. 3 1235
  20. >>> for i,j in enumerate(a):
  21. print(i,j)
  22.  
  23. 0 aaa
  24. 1 bbb
  25. 2 ccc
  26. 3 1235
  27. >>> for x in enumerate(a):
  28. print(x)
  29.  
  30. (0, 'aaa')
  31. (1, 'bbb')
  32. (2, 'ccc')
  33. (3, 1235)
  34. >>>

使用enumerate函数来统计文本行数:

文本内容:test.txt

this
is
a
test

代码:

  1. >>> for count,line in enumerate(open(r'I:\PythonTest\1234.txt','r')):
  2. count +=1
  3.  
  4. >>> print(count)
  5. 4
  6. >>>

实例2:

文本内容:1234.txt

  1. The Zen of Python, by Tim Peters
  2.  
  3. Beautiful is better than ugly.
  4. Explicit is better than implicit.
  5. Simple is better than complex.
  6. Complex is better than complicated.
  7. Flat is better than nested.
  8. Sparse is better than dense.
  9. Readability counts.
  10. Special cases aren't special enough to break the rules.
  11. Although practicality beats purity.
  12. Errors should never pass silently.
  13. Unless explicitly silenced.
  14. In the face of ambiguity, refuse the temptation to guess.
  15. There should be one-- and preferably only one --obvious way to do it.
  16. Although that way may not be obvious at first unless you're Dutch.
  17. Now is better than never.
  18. Although never is often better than *right* now.
  19. If the implementation is hard to explain, it's a bad idea.
  20. If the implementation is easy to explain, it may be a good idea.
  21. Namespaces are one honking great idea -- let's do more of those!

代码:

  1. >>> for count,line in enumerate(open(r'I:\PythonTest\1234.txt','r')):
  2. count +=1
  3. print(count,line)
  4.  
  5. 结出结果:
  6. 1 The Zen of Python, by Tim Peters
  7. 2
  8. 3 Beautiful is better than ugly.
  9. 4 Explicit is better than implicit.
  10. 5 Simple is better than complex.
  11. 6 Complex is better than complicated.
  12. 7 Flat is better than nested.
  13. 8 Sparse is better than dense.
  14. 9 Readability counts.
  15. 10 Special cases aren't special enough to break the rules.
  16. 11 Although practicality beats purity.
  17. 12 Errors should never pass silently.
  18. 13 Unless explicitly silenced.
  19. 14 In the face of ambiguity, refuse the temptation to guess.
  20. 15 There should be one-- and preferably only one --obvious way to do it.
  21. 16 Although that way may not be obvious at first unless you're Dutch.
  22. 17 Now is better than never.
  23. 18 Although never is often better than *right* now.
  24. 19 If the implementation is hard to explain, it's a bad idea.
  25. 20 If the implementation is easy to explain, it may be a good idea.
  26. 21 Namespaces are one honking great idea -- let's do more of those!
  27. >>>

Python enumerate函数的更多相关文章

  1. python enumerate() 函数的使用方法

    列表是最常用的Python数据类型,前段时间看书的时候,发现了enumerate() 函数非常实用,因为才知道下标可以这么容易的使用,总结一下. class enumerate(object): &q ...

  2. Python enumerate() 函数

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

  3. Python enumerate() 函数----枚举

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

  4. python enumerate函数用法

    enumerate函数用于遍历序列中的元素以及它们的下标 i = 0 seq = ['one', 'two', 'three'] for element in seq: print i, seq[i] ...

  5. Python enumerate() 函数笔记

    enumerate函数说明: 函数原型:enumerate(sequence, [start=0])  #第二个参数为指定索引 功能:将可循环序列sequence以start开始分别列出序列数据和数据 ...

  6. python enumerate 函数用法

    enumerate字典上是枚举.列举的意思.   C语言中关键字enum也是enumerate的缩写.   python中enumerate方法,返回一个enumerate类型.参数一般是可以遍历的的 ...

  7. [python拾遗]enumerate()函数

    在python中处理各类序列时,如果我们想显示出这个序列的元素以及它们的下标,可以使用enumerate()函数. enumerate()函数用于遍历用于遍历序列中的元素以及它们的下标,用法如下: 1 ...

  8. python之enumerate()函数的探究

    原地址:http://www.newsmth.net/nForum/#!article/Python/95860 最近用到enumerate()函数,于是查了相关的资料.           其中的一 ...

  9. 揭秘 Python 中的 enumerate() 函数

    原文:https://mp.weixin.qq.com/s/Jm7YiCA20RDSTrF4dHeykQ 如何以去写以及为什么你应该使用Python中的内置枚举函数来编写更干净更加Pythonic的循 ...

随机推荐

  1. json数据相对于xml数据.

    JSON is a valid subset of JavaScript, Python, and YAML JSON parsing is generally faster than XML par ...

  2. 数据库的事务处理必须满足ACID原则,ACID分别是指什么

    http://blog.csdn.net/dingxingmei/article/details/39270375

  3. css动态样式

    一种 var style=document.createElement("style"); style.type="text/css"; style.appen ...

  4. Qt在VS2013或Qt Creator 中的控制台输出方式设置

    首先值得注意的是:在写程序的时候,项目保存路径不要涉及到中文,否则容易出错! 一.Qt在VS2013中的控制台输出方式: 注意:这里是而不是Qt Application. 然后直接点击finish即可 ...

  5. 控件真的很好用,突然感觉自己以前研究Discuz!NT366源码的方式很2了

    控件真的很好用,突然感觉自己以前研究Discuz!NT366源码的方式很2了,就是按钮上的或其他控件上的图片哪里去了?

  6. JavaScript获取Select下拉框Option的Value和Text值的方法

    Js获取select下拉列表框各个Option的Value值相对比较容易,不过获取Text值却有点麻烦,对于一个初学JavaScript的 新手来说,可能一时还无从下手,那么就请看下本文的方法,以一个 ...

  7. 机器人操作系统ROS | 简介篇

    同样,从个人微信公众号Nao(ID:qRobotics)搬运. 前言 先放一个ROS Industrial一周年剪辑视频. ROS已经发布八周年了,在国外科研机构中非常受欢迎.目前,以美国西南研究院为 ...

  8. django(二)视图和URL配置

    创建一份视图: 在上一节,使用django-admin.py startproject制作的mysite文件夹中,创建一个叫做views.py的空文件.这个Python模块健柏寒这一章的视图. vie ...

  9. Apple Pay 应用 demo --备用哦

    "iOS8.1就已经有这个功能了,只是木有现在这么的火,现在的趋势是要火的节奏,因此很多电商平台B2B,P2P,C2C,X2X都有可能需要这个屌丝的付款功能了,在此简单的研究一下." ...

  10. MPMoviePlayerController 电影播放器—备用

    MPMoviePlayerController 与AVAudioPlayer有点类似,前者播放视频,后者播放音频,不过也有很大不同,MPMoviePlayerController 可以直接通过远程UR ...