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!
>>>
时间: 2024-10-14 14:25:46

Python enumerate函数的相关文章

Python enumerate() 函数----枚举

描述 enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中. Python 2.3. 以上版本可用,2.6 添加 start 参数. 语法 以下是 enumerate() 方法的语法: enumerate(sequence, [start=0]) 参数 sequence -- 一个序列.迭代器或其他支持迭代对象. start -- 下标起始位置. 返回值 返回 enumerate(枚举) 对象. 实例

python enumerate函数用法

enumerate函数用于遍历序列中的元素以及它们的下标 i = 0 seq = ['one', 'two', 'three'] for element in seq: print i, seq[i] i += 1 #0 one #1 two #2 three print '============' seq = ['one', 'two', 'three'] for i, element in enumerate(seq): print i, seq[i] print '===========

Python enumerate() 函数

enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中. 用大白话来说,就是给一组元素偏个号,比如我们在整理excel表格时,习惯在第一个字段加上个序号字段,写上1,2,3,4,5...一样的道理. 语法 以下是 enumerate() 方法的语法: enumerate(sequence, [start=0]) 实例 以下展示了使用 enumerate() 方法的实例: >>>seasons =

python enumerate 函数用法

enumerate字典上是枚举.列举的意思. C语言中关键字enum也是enumerate的缩写. python中enumerate方法,返回一个enumerate类型.参数一般是可以遍历的的东西,比如列表,字符串什么的. python文档中是这么说的: enumerate(sequence, [start=0]) Return an enumerate object. sequence must be a sequence, an iterator, or some other object

[python拾遗]enumerate()函数

在python中处理各类序列时,如果我们想显示出这个序列的元素以及它们的下标,可以使用enumerate()函数. enumerate()函数用于遍历用于遍历序列中的元素以及它们的下标,用法如下: 1.参数为一个元组tuple: for index, value in enumerate(('a', 'b', 'c')): '''下标,元素''' print(index,value) 2..参数为一个列表list: for index, value in enumerate([1, 2, 3])

python中enumerate函数用法

在Python中,我们习惯这样遍历: for item in sequence:    print(item) 这样遍历取不到item的序号i,所有就有了下面的遍历方法: for index in range(len(sequence)):    print(sequence[index]) 其实,如果你了解内置的enumerate函数,还可以这样写: for index, item in enumerate(sequence):    print(index, item)

python之enumerate()函数的探究

原地址:http://www.newsmth.net/nForum/#!article/Python/95860 最近用到enumerate()函数,于是查了相关的资料.           其中的一篇是这样的:一般情况下,如果要对一个列表或者数组既要遍历索引又要遍历元素时,可以用enumerate 比如: for index,value in enumerate(list):       print index,value 当然也可以 for i in range(0,len(list)): 

【python】浅谈enumerate 函数

enumerate 函数用于遍历序列中的元素以及它们的坐标: >>> for i,j in enumerate(('a','b','c')):  print i,j 0 a 1 b 2 c >>> for i,j in enumerate([1,2,3]):  print i,j 0 1 1 2 2 3 >>> for i,j in enumerate({'a':1,'b':2}):  print i,j 0 a 1 b >>> fo

python中enumerate函数的用法

描述 enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标. 也就是说,对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值. 一般用在 for 循环当中. Python 2.3. 以上版本可用,2.6 添加 start 参数. 语法 以下是 enumerate() 方法的语法: enumerate(sequence[,startindex=0]