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

(0, seq[0]), (1, seq[1]), (2, seq[2])
1
enumerate()返回的是一个enumerate对象,例如:

enumerate()使用

如果对一个列表,既要遍历索引又要遍历元素时,首先可以这样写:
list1 = ["这", "是", "一个", "测试"]
for i in range (len(list1)):
print i ,list1[i]
1
2
3
上述方法有些累赘,利用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())
1
这种方法简单,但是可能比较慢,当文件比较大时甚至不能工作。

可以利用enumerate():

count = 0
for index, line in enumerate(open(filepath,'r')):
count += 1

python enumerate用法总结的更多相关文章

  1. python enumerate用法总结【转】

    enumerate()说明 enumerate()是python的内置函数 enumerate在字典上是枚举.列举的意思 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enum ...

  2. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

  3. python enumerate用法总结--转载

    enumerate()说明 enumerate()是python的内置函数 enumerate在字典上是枚举.列举的意思 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enum ...

  4. python enumerate用法总结(转)

    原文链接:https://blog.csdn.net/churximi/article/details/51648388 enumerate()说明 enumerate()是python的内置函数en ...

  5. python enumerate用法

    含义:"枚举,列举" 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值 enumerate多 ...

  6. Python中enumerate用法详解

    enumerate()是python的内置函数.适用于python2.x和python3.xenumerate在字典上是枚举.列举的意思enumerate参数为可遍历/可迭代的对象(如列表.字符串)e ...

  7. Python内建函数enumerate()用法及在for循环应用

    Python 内建函数enumerate() 由于这个单纯很长,不容易记住,用法还是比较广泛的,下面讲述Python内建函数enumerate()用法. 1,实例 enumerate(sequence ...

  8. 为什么需要激活函数 为什么需要归一化 python内置函数:enumerate用法总结

    为什么需要激活函数 为什么需要归一化 python内置函数:enumerate用法总结 待办 激活函数的用途(为什么需要激活函数)? 如果不用激励函数(其实相当于激励函数是f(x) = x),在这种情 ...

  9. Python高级用法总结

    Python很棒,它有很多高级用法值得细细思索,学习使用.本文将根据日常使用,总结介绍Python的一组高级特性,包括:列表推导式.迭代器和生成器.装饰器. 列表推导(list comprehensi ...

随机推荐

  1. 什么是MTBF测试【转】

    本文转载自:https://blog.csdn.net/liuhaoemail/article/details/50531489 MTBF测试 目前,终端侧的可靠性测试基本上是采用称为”MTBF测试” ...

  2. ejs常用功能函数

    利用<%- include filename %>加载其他页面模版: 1.缓存功能,能够缓存已经解析好的html模版: 2.<% code %>用于执行其中javascript ...

  3. mysql学习(一)

    mysql学习,这篇博文是关于目前市面上火热的关系型数据库mysql的学习记录. 1,关于mysql的安装,自行百度. 2,sql:structure query language 结构化查询语言. ...

  4. 理解 OAuth2.0

    文章转载于阮一峰老师的博客:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html 参考文章:https://learnku.com/article ...

  5. Mac连接非22端口linux服务器

    Mac连接非22端口linux服务器 原文链接:https://www.cnblogs.com/blog5277/p/9507080.html 原文作者:博客园--曲高终和寡 1:ssh连接 打开终端 ...

  6. intelliJ idea常用快捷键 mac版

    目录 coding project coding Command + P 显示方法参数信息 Command + N 自动生成getter.setter.hashCode.equals.toString ...

  7. oledb

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data; ...

  8. 页面对象(Page Object)模式

    内容转载自 https://www.cnblogs.com/yytesting/p/6973474.html 页面对象(Page Object)模式是目前自动化测试领域普遍使用的设计模式之一,此模式可 ...

  9. vbuffer.hpp

    //vov #ifndef VBUFFER_HPP #define VBUFFER_HPP #include <iostream> #include <deque> #incl ...

  10. hdoj3251

    这题告诉我们,最小割需:满流,S断不能到T端P4126,hdoj3987 #include <iostream> #include <cstdio> #include < ...