iterfunction.py 文件源码

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

项目:nuts-flow 作者: maet3608 项目源码 文件源码
def interleave(*iterables):
    """
    Return generator that interleaves the elements of the iterables.

    >>> list(interleave(range(5), 'abc'))
    [0, 'a', 1, 'b', 2, 'c', 3, 4]

    >>> list(interleave('12', 'abc', '+-'))
    ['1', 'a', '+', '2', 'b', '-', 'c']

    :param iterable iterables: Collection of iterables, e.g. lists, range, ...
    :return: Interleaved iterables.
    :rtype: iterator
    """
    pending = len(iterables)
    fnext = lambda it: lambda: advance_iterator(it)
    nexts = itt.cycle(fnext(iter(it)) for it in iterables)
    while pending:
        try:
            for nxt in nexts:
                yield nxt()
        except StopIteration:
            pending -= 1
            nexts = itt.cycle(itt.islice(nexts, pending))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号