Try this:
for line in heapq.merge(*(map(int, file) for file in files)):
That doesn't interpret the strings as integers during comparison, but actually on-the-fly changes them to integers. The outcome is therefore integers, not strings. Can of course then be converted back to strings if desirable:
for line in map(str, heapq.merge(*(map(int, file) for file in files))):
For others / future reference: This is for Python 3, where map
returns an iterator. In Python 2, map
would need to be replaced by itertools.imap
in order to not read everything into memory at startup.