• Python内置函数(26)——enumerate


    英文文档:

    enumerate(iterablestart=0)

    Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration. The __next__() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable.

    Equivalent to:

    def enumerate(sequence, start=0):
        n = start
        for elem in sequence:
            yield n, elem
            n += 1

      

        根据可迭代对象创建枚举类型

    说明:

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

       

      1. 接受一个可迭代对象(序列或者迭代器),返回一个可枚举对象(同时返回索引和值,其中索引可以指定起始值)。

    >>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
    >>> list(enumerate(seasons))
    [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
    >>> list(enumerate(seasons, start=1)) #指定起始值
    [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
  • 相关阅读:
    English trip V1
    English trip M1
    every day a practice —— morning(5)
    English Voice of <<All Of Me>>
    bzoj 3561 DZY Loves Math VI
    luogu P4322 [JSOI2016]最佳团体
    luogu P3264 [JLOI2015]管道连接
    bzoj 5084 hashit
    luogu P6091 原根
    bzoj 5206 [Jsoi2017]原力
  • 原文地址:https://www.cnblogs.com/lincappu/p/8144816.html
Copyright © 2020-2023  润新知