iterables.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:ww 作者: Tygs 项目源码 文件源码
def starts_when(iterable, condition):
    # type: (Iterable, Union[Callable, Any]) -> Iterable
    """Start yielding items when a condition arise.

    Args:
        iterable: the iterable to filter.
        condition: if the callable returns True once, start yielding
                   items. If it's not a callable, it will be converted
                   to one as `lambda condition: condition == item`.

    Example:

        >>> list(starts_when(range(10), lambda x: x > 5))
        [6, 7, 8, 9]
        >>> list(starts_when(range(10), 7))
        [7, 8, 9]
    """
    if not callable(condition):
        cond_value = condition

        def condition(x):
            return x == cond_value
    return itertools.dropwhile(lambda x: not condition(x), iterable)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号