Another itertool function?

Andrew Dalke adalke at mindspring.com
Sun Apr 27 15:52:36 EDT 2003


Magnus Lie Hetland:
> def weave(*iterables):
>     iterables = map(iter, iterables)
>     while iterables:
>         for it in iterables[:]:
>             try:
>                 yield it.next()
>             except StopIteration:
>                 iterables.remove(it)

Being nit-picky here.  I don't like the 'remove'.  That requires
a test for equivalence in the iterators, when there's no real
need to make that assertion.  What about the following
untested code?

def weave(*iterables):
    iterables = map(iter, iterables)
    i = 0
    while iterables:
      i = i % len(iterables)
      it = iterables[i]
      try:
        yield it.next()
      except StopIteration:
        del iterables[i]
      else:
        i = i + 1

As to inclusion in itertools, that's a different question,
to which I have no comment.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list