zoukankan      html  css  js  c++  java
  • Python内置函数(44)——next

    英文文档:

    next(iterator[, default])
    Retrieve the next item from the iterator by calling its __next__() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.
    说明:
      
      1. 函数必须接收一个可迭代对象参数,每次调用的时候,返回可迭代对象的下一个元素。如果所有元素均已经返回过,则抛出StopIteration 异常。
    >>> a = iter('abcd')
    >>> next(a)
    'a'
    >>> next(a)
    'b'
    >>> next(a)
    'c'
    >>> next(a)
    'd'
    >>> next(a)
    Traceback (most recent call last):
      File "<pyshell#18>", line 1, in <module>
        next(a)
    StopIteration

      2. 函数可以接收一个可选的default参数,传入default参数后,如果可迭代对象还有元素没有返回,则依次返回其元素值,如果所有元素已经返回,则返回default指定的默认值而不抛出StopIteration 异常。

    >>> a = iter('abcd')
    >>> next(a,'e')
    'a'
    >>> next(a,'e')
    'b'
    >>> next(a,'e')
    'c'
    >>> next(a,'e')
    'd'
    >>> next(a,'e')
    'e'
    >>> next(a,'e')
    'e'
  • 相关阅读:
    堆排序
    2019晋城一中开放日
    严格次小生成树
    遥远的国度
    noip2018游记
    Luogu1736 创意吃鱼法
    P3958 奶酪
    Luogu3385 负环
    Luogu1040 加分二叉树
    Luogu1007 独木桥
  • 原文地址:https://www.cnblogs.com/sesshoumaru/p/6037922.html
Copyright © 2011-2022 走看看