http://blog.csdn.net/churximi/article/details/51648388

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 测试
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • enumerate还可以接收第二个参数,用于指定索引起始值,如:
list1 = ["这", "是", "一个", "测试"]
for index, item in enumerate(list1, 1):
print index, item
>>>
1 这
2 是
3 一个
4 测试
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

补充

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

count = len(open(filepath, 'r').readlines())
  • 1

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

可以利用enumerate():

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

【python3】 enumerate用法总结(转)的更多相关文章

  1. Python中enumerate用法详解

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

  2. python enumerate用法总结【转】

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

  3. Python3 enumerate() 函数

    Python3 enumerate() 函数  Python3 内置函数 描述 enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标 ...

  4. Python3 pandas用法大全

    Python3 pandas用法大全 一.生成数据表 1.首先导入pandas库,一般都会用到numpy库,所以我们先导入备用: import numpy as np import pandas as ...

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

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

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

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

  7. python enumerate 用法

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

  8. python3 filter用法(举例求0~n之间的素数)

    在用python3求0~n之间的素数时,关于filter用法的有点模糊,于是上网查了一下filter用法. 求0~n之间素数的脚本prime.py: def f(x): plist = [0,0] + ...

  9. enumerate用法总结-Python 3

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

随机推荐

  1. pygame系列_小球完全弹性碰撞游戏_源码下载

    之前做了一个基于python的tkinter的小球完全碰撞游戏: python开发_tkinter_小球完全弹性碰撞游戏_源码下载 今天利用业余时间,写了一个功能要强大一些的小球完全碰撞游戏: 游戏名 ...

  2. Codeforces Round #372 (Div. 1) A. Plus and Square Root 数学题

    A. Plus and Square Root 题目连接: http://codeforces.com/contest/715/problem/A Description ZS the Coder i ...

  3. Programmed Adjustable Power

    Programmed Adjustable Power I just explored an easy scheme to design a high precision programmed adj ...

  4. STM32 Timer : Base Timer, Input Capture, PWM, Output Compare

    http://www.cs.indiana.edu/~geobrown/book.pdf An example of a basic timer is illustrated in Figure 10 ...

  5. code.google.com/p/log4go 下载失败

    用 glide 下载 goim 的依赖包时报错,提示: code.google.com/p/log4go 找不到,即下载失败 主要是 code.google.com 网站已关闭导致的, 有人把它 fo ...

  6. zoj 1649

    #include <iostream> #include <queue> using namespace std; int n,m,s2,e2; int b[205][205] ...

  7. StatCounter

    StatCounter provides free customisable hit counters, visitor tracking, web analytics and website sta ...

  8. Revit Family API 创建参考平面

    使用API来编辑族时,使用doc.FamilyCreate.NewReferencePlane();创建参考平面. )         {  ];         }         // canno ...

  9. WinForm多语言版本实战项目演练

    一.课程介绍 关于如何实现“WinForm多语言版本”网上有很多实现技术方案,可以说是“琳琅满目”,"包罗万象".俗话说的好:一千个读者就有一千个哈姆雷特!如果您工作中恰好也遇到这 ...

  10. In-Place upgrade to Team Foundation Server (TFS) 2015 from TFS 2013Team Foundation Server TFS TFS 2015 TFS upgrade TFS with Sharepoint

    This upgrade document gives detailed step by step procedure for the In-Place upgrade from TFS 2013 t ...