Tuesday 3 October 2006 — This is more than 18 years old. Be careful.
Although I’ve been working in Python for a long time, every once in a while I come across a chunk of code that makes me stop and think, and admire its elegance and Pythonic nature. Raymond Hettinger’s recipe for merging multiple sorted iterators is one of those pieces of code. It’s only 29 lines including the supporting stuff, but it opened my eyes in these ways:
- It works with iterators in unusual ways, including taking a list of objects, turning them into iterators by mapping iter over them, and then iterating over the list of iterators.
- It manipulates the iterators directly, by calling the next() function explicitly. Usually I’d think about the implementation of iterators involving the next() method only when considering writing an unusual iterator, not when using an iterator.
- It uses a standard module that had escaped my attention, heapq for using a list as a sorted heap.
- It uses object methods as first-class objects that can be pushed into the heap along with the data to be sorted.
There’s nothing like a succinct thought-provoking snippet to start your day.
Comments
def merge(*iters):
iters = list(iter(i) for i in iters)
while iters:
for i in iters:
try:
yield i.next()
except StopIteration, e:
iters.remove(i)
print sorted(merge([1,2,3,5], [4,5]))
from itertools import chain
print sorted(chain([1,2,3,4], [4,5]))
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/466302
It include all the nifty stuff about using heapq and calling next() on iterators. Well, yeah, I guess I'm bragging :) At least Raymond's version is generic and not mixed with file management.
I conceed my disagreement.
Thanks,
Daniel
Add a comment: