from operator import itemgetter
def needmerge(wlist):
wstart = iter(sorted(enumerate(w.start for w in wlist), key=itemgetter(1)))
wend = iter(sorted(enumerate(w.end for w in wlist), key=itemgetter(1)))
merge = []
s = wstart.next()
e = wend.next()
try:
while True:
if e[1]-s[1] <= 500:
if s[0] != e[0]: merge.append((s[0], e[0]))
e = wend.next()
else:
s = wstart.next()
while s[1] > e[1]: e = wend.next()
except StopIteration:
pass
return merge
def domerge(windowlist):
mlist = needmerge(windowlist)
if not mlist:
return windowlist
merged = set([x[0] for x in mlist]
+ [x[1] for x in mlist])
mergelist = [w[1] for w in enumerate(windowlist) if w[0] not in merged]
for m in mlist:
mergelist.append(merge(*m))
return mergelist
def merge:
# as you described
|