美文网首页
py3笔记28:内置函数enumerate

py3笔记28:内置函数enumerate

作者: _百草_ | 来源:发表于2023-05-11 18:41 被阅读0次

enumerate [ɪˈnuːməreɪt]

class enumerate(object):
    """
    Return an enumerate object.  返回一个enum对象
    
      iterable
        an object supporting 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]), ...
    """

    def __init__(self, iterable, start=0): # known special case of enumerate.__init__
        """ Initialize self.  See help(type(self)) for accurate signature. """
        pass
# enumerate(obj)
e = enumerate("baicao")
for i in e:
    print(i, end=" ")
# (0, 'b') (1, 'a') (2, 'i') (3, 'c') (4, 'a') (5, 'o') 

# enumerate(obj,start) start表示,enum对象下标的起始位置
e = enumerate("baicao",3)
for i in e:
    print(i) 
# (3, 'b')
# (4, 'a')
# (5, 'i')
# (6, 'c')
# (7, 'a')
# (8, 'o')

相关文章

网友评论

      本文标题:py3笔记28:内置函数enumerate

      本文链接:https://www.haomeiwen.com/subject/moissdtx.html