在python中enumerate的用法多用于在for循环中得到计数,本文即以实例形式向大家展现python中enumerate的用法。具体如下:

enumerate参数为可遍历的变量,如 字符串,列表等; 返回值为enumerate类。

示例代码如下所示:

  1. import string
  2. s = string.ascii_lowercase
  3. e = enumerate(s)
  4. print s
  5. print list(e)

输出为

abcdefghijklmnopqrstuvwxyz

[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f'), (6, 'g'), (7, 'h'), (8, 'i'), (9, 'j'), (10, 'k'), (11, 'l'), (12, 'm'), (13, 'n'), (14, 'o'), (15,'p'), (16, 'q'), (17, 'r'), (18, 's'), (19, 't'), (20, 'u'), (21, 'v'), (22, 'w'), (23, 'x'), (24, 'y'), (25, 'z')]

eg:遍历一个列表,同时输出索引和元素

>>> a=['a','b','c','d','e']
>>> for index,item in enumerate(a):
...   print index,item
...
0 a
1 b
2 c
3 d
4 e

实际上enumerate返回的是一个enumerate对象

>>> enumerate(a)
<enumerate object at 0x0000000002565C60>

上面的遍历相当于这样的写法

>>> for i in range(len(a)):
...   print i,a[i]
...
0 a
1 b
2 c
3 d
4 e

python 遍历enumerate的更多相关文章

  1. 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, ...

  2. Python遍历List集合四种方法

    这篇文章主要介绍了Python 列表(List) 的四种遍历方法实例 详解的相关资料,需要的朋友可以参考下 分别是:直接遍历对象 通过索引遍历 通过enumerate方法 通过iter方法. 使用Py ...

  3. [python]python 遍历一个list 的小例子:

    [python]python 遍历一个list 的小例子: mlist=["aaa","bbb","ccc"]for ss in enume ...

  4. python中enumerate()函数用法

    python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6]  请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输 ...

  5. Python学习笔记之Python的enumerate函数

    Python 的 enumerate() 函数就像是一个神秘的黑箱,你无法简单地用一句话来概括这个函数的作用与用法. enumerate() 函数属于非常有用的高级用法,而对于这一点,很多初学者甚至中 ...

  6. 用Python遍历目录

    用Python遍历指定目录下的文件,一般有两种常用方法,但它们都是基于Python的os模块.下面两种方法基于Python2.7,主要用到的函数如下: 1.os.listdir(path):列出目录下 ...

  7. python 遍历文件夹 文件

    python 遍历文件夹 文件   import os import os.path rootdir = "d:\data" # 指明被遍历的文件夹 for parent,dirn ...

  8. python遍历目录文件脚本的示例

    例子 自己写的一个Python遍历文件脚本,对查到的文件进行特定的处理.没啥技术含量,但是也记录一下吧. 代码如下 复制代码 #!/usr/bin/python# -*- coding: utf-8 ...

  9. python遍历一个目录,输出所有文件名

    python遍历一个目录,输出所有文件名 python os模块 os import os  def GetFileList(dir, fileList):  newDir = dir  if os. ...

随机推荐

  1. UE常用快捷键使用

    进入列模式Alt+c 小写转大写Alt+F5

  2. leetcode1002

    class Solution: def commonChars(self, A: 'List[str]') -> 'List[str]': n = len(A) if n == 1: retur ...

  3. nginx 根据域名和地址跳转

    设置nginx变量 set $domail_url "$host$request_uri"; 判断并重定向if ($domail_url = '123.com/about/'){  ...

  4. day37-常见内置模块六(其他模块)

    其他内置模块 1.subprocess(系统交互)模块 2.math模块 3.zipfile模块 4.keyword模块 5....

  5. APP-10-文字识别-票据识别

    1.Postman测试 2.参数 https://cloud.baidu.com/doc/OCR/OCR-API.html#.E9.80.9A.E7.94.A8.E7.A5.A8.E6.8D.AE.E ...

  6. Druid参考配置

    pom中的maven dependency <dependency>            <groupId>com.alibaba</groupId>       ...

  7. hdoj 1004 学习思路

    hdoj 1004题目大概讲的是,将输入的字符串根据输入次数多少,输出出现次数最多的字符串. 题目逻辑很简单,就是需要选择相应的数据结构,看了别人提交的discuss,明显发现可以使用多种数据结构解这 ...

  8. delphi异常捕获try except语句 和 try finally语句用法

    原文地址:delphi try except语句 和 try finally语句用法以及区别作者:1865898133 一直写程序都没管他们,也尽量很少用,今天终于想把他给弄个明白,在网上找来,记下! ...

  9. Linux命令:history

    显示历史(执行过的)命令. history [n] history -c history -d offset history -anrw [filename] history -p arg [arg ...

  10. How to Pronounce the Numbers 1 – 10

    How to Pronounce the Numbers 1 – 10 Share Tweet Share Tagged With: Numbers Numbers are something you ...