Skip to content

Instantly share code, notes, and snippets.

@mayflaver
Created December 25, 2013 13:55
Show Gist options
  • Save mayflaver/8123388 to your computer and use it in GitHub Desktop.
Save mayflaver/8123388 to your computer and use it in GitHub Desktop.
merge multi generator
import heapq
def merge(*args):
h = []
for g in args:
try:
a = g.next()
except StopIteration:
continue
heapq.heappush(h, (a, g))
while h:
elem = heapq.heappop(h)
yield elem[0]
g = elem[1]
try:
a = g.next()
except StopIteration:
continue
heapq.heappush(h, (a, g))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment