美文网首页
python中iter()函数

python中iter()函数

作者: 帅子锅 | 来源:发表于2017-08-12 17:56 被阅读0次

    iter()函数就是生成一个迭代器,迭代器必须用__next__方法才会调用一次,如下

    >>> myTuple = (123,‘xyz’,45.67)
    >>> i = iter(myTuple)
    >>> i.next()
    123
    >>> i.next()
    ‘xyz’
    >>> i.next()
    45.67
    >>> i.next()
    Traceback (most recent call last):
    File “<stdin>”,line 1,in <module>
    StopIteration
    
    
    >>>lst = [1, 2, 3]
    >>> for i in iter(lst):
    ...     print(i)
    ... 
    1
    2
    3
    

    相关文章

      网友评论

          本文标题:python中iter()函数

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