It would be faster with ‘deque’:

def roundrobin(*iterables):
    iters = deque(map(iter,iterables), len(iterables))
    while iters:
        try:
            yield next(iters[0])
        except StopIteration:
            iters.popleft()
        else:
            iters.rotate(-1)

From: Wes Turner [mailto:wes.tur...@gmail.com]
Sent: Wednesday, November 22, 2017 04:11
To: Alon Snir <alons...@hotmail.com>
Cc: python-ideas@python.org
Subject: Re: [Python-ideas] Rewriting the "roundrobin" recipe in the itertools 
documentation

Here's toolz.itertoolz.interleave():

def interleave(seqs):
    """ Interleave a sequence of sequences
    >>> list(interleave([[1, 2], [3, 4]]))
    [1, 3, 2, 4]
    >>> ''.join(interleave(('ABC', 'XY')))
    'AXBYC'
    Both the individual sequences and the sequence of sequences may be infinite
    Returns a lazy iterator
    """
    iters = itertools.cycle(map(iter, seqs))
    while True:
        try:
            for itr in iters:
                yield next(itr)
            return
        except StopIteration:
            predicate = partial(operator.is_not, itr)
            iters = itertools.cycle(itertools.takewhile(predicate, iters))

At first glance, map should be e.g. six.moves.map for pythonic backward 
compatibility.

Is this slower than the linear time implementations listed here?

On Tuesday, November 21, 2017, Alon Snir 
<alons...@hotmail.com<mailto:alons...@hotmail.com>> wrote:
def roundrobin(*iterables):
    "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
    nexts = [ iter(it).__next__ for it in iterables ]
    i = 0
    while nexts:
        i %= len(nexts)
        try:
            yield nexts[i]()
        except StopIteration:
            del nexts[i]
        else:
            i += 1

Regards
Alon Snir
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org<javascript:;>
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to