zoukankan      html  css  js  c++  java
  • enumerate()用法

    enumerate(sequence, [start=0])

    • sequence -- 一个序列、迭代器或其他支持迭代对象。
    • start -- 下标起始位置。
    • 返回 enumerate(枚举) 对象。

    实例

    以下展示了使用 enumerate() 方法的实例:

    >>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']
    >>> list(enumerate(seasons)) [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
    >>> list(enumerate(seasons, start=1)) # 小标从 1 开始 [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

    普通的 for 循环

    >>>i = 0
    >>> seq = ['one', 'two', 'three']
    >>> for element in seq:
    ... print i, seq[i]
    ... i +=1
     
    ... 0 one
        1 two
         2 three

    for 循环使用 enumerate

    >>>seq = ['one', 'two', 'three']
    >>> for i, element in enumerate(seq):#在后边还可以设置下标的起始位置
    ... print i, element
    ... 0 one
     1 two 
     2 three
  • 相关阅读:
    HTML5元素标记释义
    Mvc使用Partial View 来封装上传控件
    订单页过滤,sql写法
    防止提交重复订单的方法
    查询数据库所有列
    asp.net 异常处理
    7. DateTime,TimeSpan
    8.1.thread
    8.2.Task
    2.2. Array
  • 原文地址:https://www.cnblogs.com/ShanCe/p/9308947.html
Copyright © 2011-2022 走看看