Skip to content

Instantly share code, notes, and snippets.

@luistung
Created May 27, 2014 04:57
Show Gist options
  • Save luistung/4b977c17d2d568a469fa to your computer and use it in GitHub Desktop.
Save luistung/4b977c17d2d568a469fa to your computer and use it in GitHub Desktop.
多路归并
import os
import sys
import heapq
fileList = [open(sys.argv[1] + '/' + f) for f in os.listdir(sys.argv[1])]
def lineIterator(f):
for line in f:
line = line.rstrip()
tokens = line.split('\t')
if len(tokens) <> 3:continue
key = (tokens[0], tokens[1])
yield (key, line)
for line in heapq.merge(*map(lineIterator, fileList)):
print line[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment