python中enumerate的使用

在python的应用中,当我们使用一个数组或者列表的时候既要遍历索引又要遍历元素的时候通常的做法是这样的:

>>> lsi = [1,2,3,4,5,6,7,8,9]
>>> lsi
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for i in range(0,len(lsi)):
...     print i ,lsi[i]
...
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
>>> 

使用 内置enumerrate函数会有更加直接,优美的做法,先看看enumerate的定义:

def enumerate (collection ):
    ‘Generates an indexed series:  (0,coll[0]), (1,coll[1]) ...‘
     i = 0
     it = iter (collection )
     while 1 :
     yield (i , it . next ())
     i += 1 

enumerate会将数组或列表组成一个索引序列。使我们在获取索引和索引内容的时候更加方便。

演示如下:

>>> list
[1, 2, 3, 4, 5, 6, 7, 8, 9, 41, 56]
>>> for index,text in enumerate(list):
...     print index , text
...
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 41
10 56
>>> 

在 cookbook里介绍,如果你要计算文件的行数,可以这样写:

count = len (open (thefilepath , ‘ rU ’ ). readlines ())

前面这种方法简单,但是可能比 较慢,当文件比较大时甚至不能工作,下面这种循环读取的方法更合适些。

Count = - 1
For count , line in enumerate (open (thefilepath , ‘ rU ’ )):
    Pass
Count += 1
时间: 2024-08-30 03:24:40

python中enumerate的使用的相关文章

python中enumerate()的用法

先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6]  请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输出, 2.将 list 倒序成 [6, 5, 4, 3, 2, 1] 3.将a 中的偶数挑出 *2 ,结果为 [4, 8, 12] 这个例子用到了python中enumerate的用法.顺便说一下enumerate在for循环中得到计数的用法,enumerate参数为可遍历的变量,如 字符串,列表等: 返回值为enumerate类. 示例代码如

python中enumerate()的用法

先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6]  请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输出, 2.将 list 倒序成 [6, 5, 4, 3, 2, 1] 3.将a 中的偶数挑出 *2 ,结果为 [4, 8, 12] 这个例子用到了python中enumerate的用法.顺便说一下enumerate在for循环中得到计数的用法,enumerate参数为可遍历的变量,如 字符串,列表等: 返回值为enumerate类. 示例代码如

python中enumerate()函数用法

python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6]  请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输出, 2.将 list 倒序成 [6, 5, 4, 3, 2, 1] 3.将a 中的偶数挑出 *2 ,结果为 [4, 8, 12] 这个例子用到了python中enumerate的用法.顺便说一下enumerate在for循环中得到计数的用法,enumerate参数为可遍历的变量,如 字符串,列表等

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、变量类型转换

enumerate可以在遍历过程中自动生成新的一列并从0开始计数 1 a = ["hello", "world", "dlrb"] 2 for key, b in enumerate(a, 1): 3 print key, b 我们定义了一个集合a,我们现在想要输出集合a中的每一个元素并且从数字1开始编号,输出结果: 1 hello 2 world 3 dlrb 上面的代码中我们定义了一个变量key,enumerate(a, 1)的意思是在enu

python中enumerate函数的用法

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

Python中enumerate用法详解

enumerate()是python的内置函数.适用于python2.x和python3.xenumerate在字典上是枚举.列举的意思enumerate参数为可遍历/可迭代的对象(如列表.字符串)enumerate多用于在for循环中得到计数,利用它可以同时获得索引和值,即需要index和value值的时候可以使用enumerateenumerate()返回的是一个enumerate对象 >>> lst = [1, 2, 3, 4, 10, 5] >>> enumer

python中enumerate、xrange、range

enumerate可以给列表自动生成一列,默认从0开始,自动增长1,可以指定默认开始值 list_product = ["thinkpad","macbook","iphone8","robbit"] for key,v in enumerate(list_product,1): print(key,v) range 2.7中的用来获取指定范围内的数1.range(0,1000000),内存中一次创建所有的数据2.xrange

python中enumerate()的用法详解【转摘】

enumerate()是python的内置函数.适用于python2.x和python3.xenumerate在字典上是枚举.列举的意思enumerate参数为可遍历/可迭代的对象(如列表.字符串)enumerate多用于在for循环中得到计数,利用它可以同时获得索引和值,即需要index和value值的时候可以使用enumerateenumerate()返回的是一个enumerate对象 >>> lst = [1, 2, 3, 4, 10, 5] >>> enumer