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的循 ...
随机推荐
- oracle登陆连接的问题
一.登陆 1.使用客户端 直接在database中配置: IP:1521/orcl 其中IP为要连接的IP 其中1521为要连接的数据库的端口 其中orcl为要连接的数据库的实例名字 2.使用命令行 ...
- iOS 8 自动布局sizeclass和autolayout的基本使用
1.首先创建新的工程,设置rootviewcontroller(这里不再多说) 2.勾选下面(因为我们到下面是使用sizeClass,所以勾选两个): 3.这里我创建了一个lable,名称为View1 ...
- 使用AFNetworking时, 控制器点击返回销毁了, 但还是会执行请求成功或失败的block, 导致野指针异常
原本我以为是我程序框架有问题...后来才知道, 无知真可怕... __unsafe_unretained __block typeof(self) weakSelf = self; AFHTTPSes ...
- 用CALayer实现淡入淡出的切换图片效果
由于直接更改layer的contents显示的隐式动画切换的时候的动画持续时间没办法控制, 切换效果不尽人意,所以这里配合了CABasicAnimation实现淡入淡出的切换效果, 另外还可以使用组合 ...
- (七)Angularjs - 控制器
控制器的作用 没有控制器/controller,我们没有地方定义业务模型 比如:ng-init指令.我们可以使用ng-init指令在scope对象上定义数据 <div ng-init=" ...
- eclipse提交hadoop集群跑程序
在eclipse下搭建hadoop后,测试wordcount程序,右击 Run on hadoop 程序跑成功后,发现“INFO - Job job_local401325246_0001 compl ...
- Linux(CentOS)搭建SVN服务器全攻略
虽然在windows上搭建SVN很简单,但是效能却不高,这当然是和linux相比了.然而在linux上搭建SVN却非常繁琐,所以今天这篇文章就来一步一步教您如何在Centos上搭建SVN 安装#yum ...
- MySQL新建用户,授权,删除用户,修改密码总结
首先要声明一下:一般情况下,修改MySQL密码,授权,是需要有mysql里的root权限的. 注:本操作是在WIN命令提示符下,phpMyAdmin同样适用. 用户:rdingcn 用户数据库:rdi ...
- C程序设计语言练习题1-20
练习1-20 编写程序detab,将输入中的制表符替换成适当数目的空格,使空格充满到下一个制表符终止位的地方.假设制表符终止位的位置是固定的,比如每隔n列就会出现一个制表符终止位.n应该是变量还是符号 ...
- web Service试用简例
1.打开文件,选择新建Asp.Net web服务. 2.出现新建页面如下. using System; using System.Collections.Generic; using System.L ...