参考链接: Python中的enumerate

enumerate英文翻译为枚举的意思。 可以将一个可遍历的数据对象组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。Python 2.3. 以上版本可用,2.6 添加 start 参数。

语法

>>> help(enumerate)

Help on class enumerate in module __builtin__:

class enumerate(object)

|  enumerate(iterable[, start]) -> iterator for index, value of iterable

|

|  Return an enumerate object.  iterable must be another object that supports

|  iteration.  The enumerate object yields pairs containing a count (from

|  start, which defaults to zero) and a value yielded by the iterable argument.

|  enumerate is useful for obtaining an indexed list:

|      (0, seq[0]), (1, seq[1]), (2, seq[2]), ...

参数

sequence       # 一个序列、迭代器或其他支持迭代对象。start                # 下标起始位置、默认为0

返回值

返回枚举类型

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']

>>> list(enumerate(seasons))                # 默认start为0

[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]

>>> list(enumerate(seasons, start=1))       # start为1

[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

>>> s = "ABCD"

>>> list(enumerate(s))                      # 字符串类型

[(0, 'A'), (1, 'B'), (2, 'C'), (3, 'D')]

>>> l = ["A", "B", "C", "D"]

>>> list(enumerate(l))                      # 列表类型

[(0, 'A'), (1, 'B'), (2, 'C'), (3, 'D')]

>>> t = ("A", "B", "C", "D")

>>> list(enumerate(t))                      # 元祖类型

[(0, 'A'), (1, 'B'), (2, 'C'), (3, 'D')]

>>> d = {"A": 1, "B": 2}

>>> list(enumerate(d))                      # 字典类型,枚举其key

[(0, 'A'), (1, 'B')]

>>>

通过for循环遍历示例

>>> # 通过元素来遍历

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']

>>> count = 0

>>> for season in seasons:

...     print count,"=>", season

...     count += 1

...

0 => Spring

1 => Summer

2 => Fall

3 => Winter

>>> # 通过索引来遍历

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']

>>> for i in range(len(seasons)):

...     print i, "=>", seasons[i]

...

0 => Spring

1 => Summer

2 => Fall

3 => Winter

>>>

通过enumerate来遍历示例

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']

>>> for (index, season) in enumerate(seasons):

...     print index,"=>",season

...

0 => Spring

1 => Summer

2 => Fall

3 => Winter

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']

>>> for (index, season) in enumerate(seasons, start=1):

...     print index,"=>",season

...

1 => Spring

2 => Summer

3 => Fall

4 => Winter

>>>

其它示例:统计文本的行数

通常的做法

>>> count = len(open(r"D:\\1.txt").readlines())

>>> count

7

使用enumerate

>>> for index,line in enumerate(open(r"D:\\1.txt"), start=1):

...     pass

...

>>> index

7

[转载] Python中的enumerate函数介绍相关推荐

  1. Python中numpy.power()函数介绍

    Python中numpy.power()函数介绍 power(x, y) 函数,计算 x 的 y 次方. 示例: x 和 y 为单个数字: import numpy as npprint(np.pow ...

  2. 【Python常用函数】一文让你彻底掌握Python中的enumerate函数

    任何事情都是由量变到质变的过程,学习Python也不例外.只有把一个语言中的常用函数了如指掌了,才能在处理问题的过程中得心应手,快速地找到最优方案.本文和你一起来探索Python中的enumerate ...

  3. python中的enumerate 函数(编号的实现方式)

    enumerate 函数用于遍历序列中的元素以及它们的下标: 默认从0开始,如果想从1开始,可以仿照最后案例 加上逗号,和数字编号 >>> for i,j in enumerate( ...

  4. python中的enumerate函数

    enumerate 函数用于遍历序列中的元素以及它们的下标: >>> for i,j in enumerate(('a','b','c')):  print i,j 0 a 1 b ...

  5. [转载] python中numpy.concatenate()函数的使用

    参考链接: Python中的numpy.append numpy库数组拼接np.concatenate 原文:https://blog.csdn.net/zyl1042635242/article/d ...

  6. python中的enumerate()函数

    1 enumerate()简介及语法: enumerate()是Python中的内置函数,用于可迭代/可遍历的的数据对象组合为一个索引序列,同时列出数据和数据下标,一般用在for循环中. enumer ...

  7. Python 中的 enumerate 函数

    enumerate 函数用于遍历序列中的元素以及它们的下标: >>> for i,j in enumerate(('a','b','c')):print i,j 0 a 1 b 2 ...

  8. python中的enumerate函数用于遍历序列中的元素以及它们的下标

    enumerate 函数用于遍历序列中的元素以及它们的下标: >>> for i,j in enumerate(('a','b','c')):  print i,j 0 a 1 b ...

  9. [转载] python中callable_Python callable() 函数

    参考链接: Python callable() 转自:快递小可 Python callable() 函数 描述 python中的内建函数callable( ), 可以检查一个对象是否是可调用的 . 对 ...

最新文章

  1. 第十七届全国大学生智能车竞赛智能视觉组培训第一弹
  2. Servlet 生命周期、工作原理
  3. oracle表没有数据判断,Oracle中判断有表DROP无表CREATE的存储过程
  4. 在python中可以用什么关键字来声明一个类_3. Python基础语法
  5. web.config forms节点中的属性的含义和用途
  6. Linux自动删除n天前备份
  7. CommonJs、AMD 、CMD 前端开发模块化组合
  8. 阶段3 2.Spring_08.面向切面编程 AOP_6 四种常用通知类型
  9. 学海无涯!马士兵的Java教程
  10. 光电信息科学与工程学c语言吗,光电信息科学与工程是热门吗?本文讲给你讲个透彻...
  11. Python:实现random forest regressor随机森林回归器算法(附完整源码)
  12. SQL Server 搭建Northwind详细教程
  13. linux服务器单向ping不通,Linux下的单向ping通问题
  14. Win10样式管理与夜间模式
  15. Python&Opencv手势识别系统
  16. NAS硬盘存储服务器维修,NAS存储服务器用NAS硬盘的原因有哪些?NAS存储硬盘该如何选择?...
  17. 拥有一把工科生专属的尺子
  18. 京东商城关键词SEO优化总结
  19. 什么是Gamma 曲线
  20. 恋爱小女孩给男朋友写的信

热门文章

  1. 【luogu3834】【模板】可持久化线段树 2(主席树),静态区间第K小值
  2. 计算机科学基础word实验一,大学计算机基础综合实验实验报告 参考模板(1)
  3. 中国科学院大学计算机研究生拟录取名单,2019中国科学院大学硕士考研研究生拟录取名单全部(2)...
  4. 真正的帅哥没人说帅_“浩南哥”这话你敢信?郑伊健:在香港,没人夸我帅
  5. 无类域间路由CIDR
  6. poj 2376 bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班(贪心)
  7. 51nod-1065:最小正子段和
  8. Undefined function 'adb' for input arguments of type 'double'.
  9. pytorch 中 torch.cat 函数的使用
  10. python中利用pygame模块输出文字