If your files are not very large, just read them all in memory (as S. Lott suggests). It will definitely be easier.
, "" . , , , , heapq.merge. , , , .
import heapq
import contextlib
class Domain(object):
def __init__(self,domain):
self.domain=domain
@property
def tld(self):
return self.domain.split('.',1)[0]
def __lt__(self,other):
return self.tld<=other.tld
def __str__(self):
return self.domain
class DomFile(file):
def next(self):
return Domain(file.next(self).strip())
filenames=('data1.txt','data2.txt')
with contextlib.nested(*(DomFile(filename,'r') for filename in filenames)) as fhs:
for elt in heapq.merge(*fhs):
print(elt)
data1.txt:
google.com
stackoverflow.com
yahoo.com
data2.txt:
standards.freedesktop.org
www.imagemagick.org
:
google.com
stackoverflow.com
standards.freedesktop.org
www.imagemagick.org
yahoo.com