from heapq import heappush, heappop
alist = []
l = [('A',1),('B',2),('F',6)]
alist.append(l)
l = [('C',3),('D',4),('H',8)]
alist.append(l)
l = [('E',5),('G',7)]
alist.append(l)
#print alist
heap = []
index = 0
for lst in alist:
if lst:
item = lst.pop(0);
heappush(heap, (item, index))
index = index + 1
while heap:
ituple = heappop(heap)
item = ituple[0]
index = ituple[1]
lst = alist[index]
if lst:
newitem = lst.pop(0)
heappush(heap, (newitem, index))
print item
Sunday, June 15, 2008
Muti-list merge in Python
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment