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的循 ...
随机推荐
- 设计模式:模版模式(Template Pattern)
android中的Activity框架,View框架中大量的on函数基本上都应用到了Template模式,掌握这一模式对于理解这些框架大有裨益. 模版模式 又叫模板方法模式,在一个方法中定义一个算法的 ...
- iOS更改ShareSDK默认的分享功能界面
ShareSDK的集成这里就不详细介绍了, 官网的都已经够详细了.. 官方的默认分享样式如下: 贴上我的源代码: // 创建分享图片 NSString *imageURLString = @" ...
- Ubuntu 安装之后的配置博文总结
由于频繁地在各种机器上给别人安装ubuntu,每次安装之后都需要进行一些配置,现在以ubuntu12.04为例,就他人的一些配置博文总结如下: 1. Ubuntu安装中文输入法 http://www. ...
- Ubuntu下搭建本地WordPress站点
想在本地搭建WordPress博客站点作测试用?本教程一步一步教您在Linux上搭建一个LAMP(Linux, Apache, MySQL, PHP)服务器并部署WordPress博客. 请注意在复制 ...
- Windows 7 Apache下计算机无法访问局域网网站的问题
在Windows 7系统下,由于安全限制问题,本机搭建的网站,局域网内其它计算机是无法访问的. 要解决这个问题,只需要在防火墙加入自建规则便可以了,具体步骤如下: 控制面板=>Windows防火 ...
- Java在ACM中的使用
1.基本框架 import java.oi.*; import java.util.* public class Main { public static void main(St ...
- php加密解密实用类
一个加解密类.如果你想在用户忘记密码时为他或她找回原来的密码,那么这个类是个好用的工具 用户注册的密码一般不会明文保存,总得加个密先.最简单的当然是在数据库sql语句中调用md5函数加密用户密码.这里 ...
- FileReader
Reader 字符流的基类,抽象类 FileReader 读取文件的字符流 package file; import java.io.File; import java.io.FileReader; ...
- js 解析 json
1.简单的json格式 { "user": [ { "name":"name1", "age":24, "se ...
- Linux_jdk path (execute and install)
作者:潇湘隐者 出处:http://www.cnblogs.com/kerrycode/ 1:echo $JAVA_HOME 使用$JAVA_HOME的话能定位JDK的安装路径的前提是配置了环境变量$ ...