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. libthrift0.9.0解析(二)之TSimpleServer

    TSimpleServer简单实现Tserver,代码如下. /** * Simple singlethreaded server for testing. * */ public class TSi ...

  2. CRT内存调试标记

    static unsigned char _bNoMansLandFill = 0xFD; /* fill no-man's land with this */ static unsigned cha ...

  3. 标准SQL语言的用法

    原文链接:http://www.ifyao.com/2015/05/18/%E6%A0%87%E5%87%86%E7%9A%84sql%E8%AF%AD%E8%A8%80%E4%BD%BF%E7%94 ...

  4. Memcache的基本应用

    $mc = new Memcache(); $mc->connect('127.0.0.1', 11211); $sql = "select * from user where id= ...

  5. php开发通用采集程序

    php采集程序构建基本步骤: 采集程序是什么?获取远程数据(文字.图片.图片)并快速保存到本地或指定地址. 如天气预报(小偷程序): 远程获取-->替换内容-->展示给用户 如实时更新的新 ...

  6. Visual Studio调试之断点进阶篇

    Visual Studio调试之断点进阶篇 在上一篇文章Visual Studio调试之断点基础篇里面介绍了什么是断点,INT 是Intel系列CPU的一个指令,可以让程序产生一个中断或者异常.程序中 ...

  7. Application.Count.ToString()和Application["count"].ToString()的区别

    当属性名中包括特殊字符如 “.”或“-”就不能使用“.”操作符了.操作符只能使用[ ]操作符 为了统计网站的在线人数,我们可以在Global.asa文件中包含如下代码: <SCRIPT LANG ...

  8. [POJ] 1606 Jugs(BFS+路径输出)

    题目地址:http://poj.org/problem?id=1606 广度优先搜索的经典问题,倒水问题.算法不需要多说,直接BFS,路径输出采用递归.最后注意是Special Judge #incl ...

  9. Android对ScrollView滚动监听,实现美团、大众点评的购买悬浮效果

    转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17761431),请尊重他人的辛勤劳动成果,谢谢! 我之前写 ...

  10. IBM总裁郭士纳总结的四类人

    IBM总裁郭士纳总结的四类人, 您属于哪一种呢-欢迎就此话题发表评论 积极采取行动促使事件发生的人 被动接受所发生事件的人 对事件持旁观者心态的人 什么事都不关心的人