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函数的更多相关文章

  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. msi软件包无法安装

    安装某些msi软件包,提示“This advertised application will not be installed because it might be unsafe. Contact ...

  2. Maven构建SSM架构,并分离层次,使用Maven 组织多项目

    参考http://www.cnblogs.com/quanyongan/archive/2013/05/28/3103243.html一步一步搭建,感谢QuantSeven 1.建好的目录结构 说明: ...

  3. 【转】深入理解Java内存模型(三)——顺序一致性

    数据竞争与顺序一致性保证 当程序未正确同步时,就会存在数据竞争.java内存模型规范对数据竞争的定义如下: 在一个线程中写一个变量, 在另一个线程读同一个变量, 而且写和读没有通过同步来排序. 当代码 ...

  4. 关于Visual Studio中的TraceDebugging文件夹

    最近一段时间发现C盘莫名其妙的变小了,各种清理各种卸载还是没用,电脑慢的是在无法使用 .最后只能一个文件夹一个文件夹的找,最后针对“C:\Documents and Settings\All User ...

  5. php访问方法外变量

    class Capture { private static $_CapSite = 222; function dd() { echo self::$_CapSite; } } $cc=new Ca ...

  6. 搭建ngrok服务器(ubuntu 14)-- 微信 80端口和IPC备案限制解决方案

    概述: ngrok其实这东西,我也不是很懂,所以也直接跟大家说,这就是个类似花生壳的东西. 简单来说,它就好像把我们内网自己使用的电脑和服务器用vpn连接起来,然后你的电脑就可以从互联网来访问了,有个 ...

  7. VS2013 编译 MySql Connector C 6.1.6

    1.下载cmake http://cmake.org/ 2.下载最新版MySql Connector C http://www.mysql.com 3.命令行下,转到源代码目录下,"cmak ...

  8. 阿里云服务器上使用iptables设置安全策略

    转自:http://www.netingcn.com/aliyun-iptables.html 公司的产品一直运行在云服务器上,从而有幸接触过aws的ec2,盛大的云服务器,最近准备有使用阿里云的弹性 ...

  9. 《head first python》第一章《初识python》总结

    该章主要介绍了python的安装和列表相关 列表主要用了movies做介绍: 1.列表及嵌套列表:movies=["the hodaliy",1975,"zhangyim ...

  10. win7禁止自动使用浏览器打开FTP而是用资源管理器

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\ftp] @="URL:File Transfer Protocol" ...