python enumerate用法总结

enumerate()说明

  • enumerate()是python的内置函数
  • enumerate在字典上是枚举、列举的意思
  • 对于一个可迭代的(iterable)/可遍历的对象(如列表、字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值
  • enumerate多用于在for循环中得到计数
  • 例如对于一个seq,得到:
    (0, seq[0]), (1, seq[1]), (2, seq[2])
  • enumerate()返回的是一个enumerate对象,例如: 

enumerate()使用

  • 如果对一个列表,既要遍历索引又要遍历元素时,首先可以这样写:
list1 = ["这", "是", "一个", "测试"]
for i in range (len(list1)):
    print i ,list1[i]
  • 上述方法有些累赘,利用enumerate()会更加直接和优美:
list1 = ["这", "是", "一个", "测试"]
for index, item in enumerate(list1):
    print index, item
>>>
0 这
1 是
2 一个
3 测试
  • enumerate还可以接收第二个参数,用于指定索引起始值,如:
list1 = ["这", "是", "一个", "测试"]
for index, item in enumerate(list1, 1):
    print index, item
>>>
1 这
2 是
3 一个
4 测试

补充

如果要统计文件的行数,可以这样写:

count = len(open(filepath, ‘r‘).readlines())

这种方法简单,但是可能比较慢,当文件比较大时甚至不能工作。

可以利用enumerate():

count = -1
for index, line in enumerate(open(filepath,‘r‘)):
    count += 1
时间: 2024-08-03 15:28:17

python enumerate用法总结的相关文章

python enumerate 用法

A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , where thing is either an iterator or a sequence, returns a iterator that will return (0, thing [0]) , (1, thing [1]) , (2, thing [2]) , and so forth. A c

python enumerate用法总结(转)

原文链接:https://blog.csdn.net/churximi/article/details/51648388 enumerate()说明 enumerate()是python的内置函数enumerate在字典上是枚举.列举的意思对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值enumerate多用于在for循环中得到计数例如对于一个seq,得到: (0, seq[0]), (1, seq[1]),

python enumerate用法

在同时需要用到index和value值的时候可以用到enumerate,参数为可遍历的变量,如字符串,列表等,返回enumerate类. 例: import string s = string.ascii_lowercase e = enumerate(s) print s print list(e) 输出结果为: [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f'), (6, 'g'), (7, 'h'), (8, 'i'),

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()用法及在for循环应用

Python 内建函数enumerate() 由于这个单纯很长,不容易记住,用法还是比较广泛的,下面讲述Python内建函数enumerate()用法. 1,实例 enumerate(sequence, [start=0]) 2,enumerate()用法 >>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']>>> list(enumerate(seasons))[(0, 'Spring'), (1, 'Summer')

Python高级用法总结

Python很棒,它有很多高级用法值得细细思索,学习使用.本文将根据日常使用,总结介绍Python的一组高级特性,包括:列表推导式.迭代器和生成器.装饰器. 列表推导(list comprehensions) 场景1:将一个三维列表中所有一维数据为a的元素合并,组成新的二维列表. 最简单的方法:新建列表,遍历原三维列表,判断一维数据是否为a,若为a,则将该元素append至新列表中. 缺点:代码太繁琐,对于Python而言,执行速度会变慢很多. 针对场景1,我们首先应该想到用列表解析式来解决处理

Python Dict用法

Operation Result len(a) the number of items in a 得到字典中元素的个数 a[k] the item of a with key k 取得键K所对应的值 a[k] = v set a[k] to v 设定键k所对应的值成为v del a[k] remove a[k] from a 从字典中删除键为k的元素 a.clear() remove all items from a 清空整个字典 a.copy() a (shallow) copy of a 得

python with用法

@python with用法 python中with可以明显改进代码友好度,比如: [python] view plaincopyprint? with open('a.txt') as f: print f.readlines() 为了我们自己的类也可以使用with, 只要给这个类增加两个函数__enter__, __exit__即可: [python] view plaincopyprint? >>> class A: def __enter__(self): print 'in e

(转)python set 用法

转载自:http://hi.baidu.com/????_xu/blog/item/5b9650c513bd3f049d163d8b .html python的set和其他语言类似, 是一个 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算. >>> basket = [’apple’, ’orange’, ’apple’, ’pear’