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中查询表的大小、表的占用情况和表空间的大小
转载自http://blog.csdn.net/cuker919/article/details/8514253 select segment_name, bytes as 大小 from user_ ...
- 常用hash函数
常用的哈希函数 通用的哈希函数库有下面这些混合了加法和一位操作的字符串哈希算法.下面的这些算法在用法和功能方面各有不同,但是都可以作为学习哈希算法的实现的例子. 1.RS 从Robert S ...
- iOS 8 自动布局sizeclass和autolayout的基本使用
1.首先创建新的工程,设置rootviewcontroller(这里不再多说) 2.勾选下面(因为我们到下面是使用sizeClass,所以勾选两个): 3.这里我创建了一个lable,名称为View1 ...
- xAML中一些控件的用法学习
首先,介绍一些比较简单的设计,这些可以直接通过拖拽实现.如下例子: <Window x:Class="wpf1.MainWindow" xmlns="http:// ...
- Java集合类笔试题
介绍Collection框架的结构 Collection接口是单值存放的最大父接口,可以向其中保存多个单值的数据(所谓单值就是指集合中的每个元素都是一个对象).其子接口主要有:List.Set.Que ...
- Ubuntu 13.10 Mono安装历程
这两天捣鼓一下Mono,记录一下自己所遇到的问题,已经解决办法 首先,先打开gnome-terminal 1. 先更新的一下系统 sudo apt-get update sudo apt-get up ...
- apache2.2 虚拟主机配置详解
一.修改httpd.conf 打开appserv的安装目录,找到httpd.conf文件,分别去掉下面两行文字前面的#号. #LoadModule vhost_alias_module modules ...
- Phpcms v9系统类库与函数库调用方法
在分享了n多phpcms的教程后,cmsyou继续分享关于phpcms v9系统类库与函数库的调用方法. 系统类库位于系统的 /libs/functions目录下面,函数库文件名为*.func.php ...
- python 基础,包括列表,元组,字典,字符串,set集合,while循环,for循环,运算符。
1.continue 的作用:跳出一次循环,进行下一次循环 2.break 跳出不再循环 3.常量 (全是大写)NAME = cjk 一般改了会出错 4.py ...
- 深入理解querySelector(All)
querySelector和querySelectorAll同属于Selectors API Level 1规范,该规范早在2006年就已经开始发展,并在2007年10月形成querySe ...