Python enumerate函数
enumerate函数接受一个可遍历的对象,如列表、字符串,可同时遍历下标(index)及元素值(value)
- >>> a = ['aaa','bbb','ccc',1235]
- >>> print(a)
- ['aaa', 'bbb', 'ccc', 1235]
- >>> print(len(a))
- 4
- >>> for i in range(len(a)):
- print(i)
- 0
- 1
- 2
- 3
- >>> for j in range(len(a)):
- print(j,a[j])
- 0 aaa
- 1 bbb
- 2 ccc
- 3 1235
- >>> for i,j in enumerate(a):
- print(i,j)
- 0 aaa
- 1 bbb
- 2 ccc
- 3 1235
- >>> for x in enumerate(a):
- print(x)
- (0, 'aaa')
- (1, 'bbb')
- (2, 'ccc')
- (3, 1235)
- >>>
使用enumerate函数来统计文本行数:
文本内容:test.txt
this
is
a
test
代码:
- >>> for count,line in enumerate(open(r'I:\PythonTest\1234.txt','r')):
- count +=1
- >>> print(count)
- 4
- >>>
实例2:
文本内容:1234.txt
- The Zen of Python, by Tim Peters
- Beautiful is better than ugly.
- Explicit is better than implicit.
- Simple is better than complex.
- Complex is better than complicated.
- Flat is better than nested.
- Sparse is better than dense.
- Readability counts.
- Special cases aren't special enough to break the rules.
- Although practicality beats purity.
- Errors should never pass silently.
- Unless explicitly silenced.
- In the face of ambiguity, refuse the temptation to guess.
- There should be one-- and preferably only one --obvious way to do it.
- Although that way may not be obvious at first unless you're Dutch.
- Now is better than never.
- Although never is often better than *right* now.
- If the implementation is hard to explain, it's a bad idea.
- If the implementation is easy to explain, it may be a good idea.
- Namespaces are one honking great idea -- let's do more of those!
代码:
- >>> for count,line in enumerate(open(r'I:\PythonTest\1234.txt','r')):
- count +=1
- print(count,line)
- 结出结果:
- 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!
- >>>
Python enumerate函数的更多相关文章
- python enumerate() 函数的使用方法
列表是最常用的Python数据类型,前段时间看书的时候,发现了enumerate() 函数非常实用,因为才知道下标可以这么容易的使用,总结一下. class enumerate(object): &q ...
- Python enumerate() 函数
描述 enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中. Python 2.3. 以上版本可用,2. ...
- Python enumerate() 函数----枚举
描述 enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中. Python 2.3. 以上版本可用,2. ...
- python enumerate函数用法
enumerate函数用于遍历序列中的元素以及它们的下标 i = 0 seq = ['one', 'two', 'three'] for element in seq: print i, seq[i] ...
- Python enumerate() 函数笔记
enumerate函数说明: 函数原型:enumerate(sequence, [start=0]) #第二个参数为指定索引 功能:将可循环序列sequence以start开始分别列出序列数据和数据 ...
- python enumerate 函数用法
enumerate字典上是枚举.列举的意思. C语言中关键字enum也是enumerate的缩写. python中enumerate方法,返回一个enumerate类型.参数一般是可以遍历的的 ...
- [python拾遗]enumerate()函数
在python中处理各类序列时,如果我们想显示出这个序列的元素以及它们的下标,可以使用enumerate()函数. enumerate()函数用于遍历用于遍历序列中的元素以及它们的下标,用法如下: 1 ...
- python之enumerate()函数的探究
原地址:http://www.newsmth.net/nForum/#!article/Python/95860 最近用到enumerate()函数,于是查了相关的资料. 其中的一 ...
- 揭秘 Python 中的 enumerate() 函数
原文:https://mp.weixin.qq.com/s/Jm7YiCA20RDSTrF4dHeykQ 如何以去写以及为什么你应该使用Python中的内置枚举函数来编写更干净更加Pythonic的循 ...
随机推荐
- json数据相对于xml数据.
JSON is a valid subset of JavaScript, Python, and YAML JSON parsing is generally faster than XML par ...
- 数据库的事务处理必须满足ACID原则,ACID分别是指什么
http://blog.csdn.net/dingxingmei/article/details/39270375
- css动态样式
一种 var style=document.createElement("style"); style.type="text/css"; style.appen ...
- Qt在VS2013或Qt Creator 中的控制台输出方式设置
首先值得注意的是:在写程序的时候,项目保存路径不要涉及到中文,否则容易出错! 一.Qt在VS2013中的控制台输出方式: 注意:这里是而不是Qt Application. 然后直接点击finish即可 ...
- 控件真的很好用,突然感觉自己以前研究Discuz!NT366源码的方式很2了
控件真的很好用,突然感觉自己以前研究Discuz!NT366源码的方式很2了,就是按钮上的或其他控件上的图片哪里去了?
- JavaScript获取Select下拉框Option的Value和Text值的方法
Js获取select下拉列表框各个Option的Value值相对比较容易,不过获取Text值却有点麻烦,对于一个初学JavaScript的 新手来说,可能一时还无从下手,那么就请看下本文的方法,以一个 ...
- 机器人操作系统ROS | 简介篇
同样,从个人微信公众号Nao(ID:qRobotics)搬运. 前言 先放一个ROS Industrial一周年剪辑视频. ROS已经发布八周年了,在国外科研机构中非常受欢迎.目前,以美国西南研究院为 ...
- django(二)视图和URL配置
创建一份视图: 在上一节,使用django-admin.py startproject制作的mysite文件夹中,创建一个叫做views.py的空文件.这个Python模块健柏寒这一章的视图. vie ...
- Apple Pay 应用 demo --备用哦
"iOS8.1就已经有这个功能了,只是木有现在这么的火,现在的趋势是要火的节奏,因此很多电商平台B2B,P2P,C2C,X2X都有可能需要这个屌丝的付款功能了,在此简单的研究一下." ...
- MPMoviePlayerController 电影播放器—备用
MPMoviePlayerController 与AVAudioPlayer有点类似,前者播放视频,后者播放音频,不过也有很大不同,MPMoviePlayerController 可以直接通过远程UR ...