随学随记
用heappush处理key-value键值对元素。
heappush可以对key-value键值对格式排序,key-value序对必须是统一格式(比如都是list或都是tuple)
heappush以key-value键值对的第一个元素为key进行排序,后面无论有多少个元素都视为value
举例来说,
from heapq import *
list1 = [[1,2,3],[2,3],[3,4],[1,1]]
list2 = []
for i in range(len(list1)):
heappush(list2,list1[i])
for i in range(len(list2)):
print(heappop(list1))
>>>
[1, 2, 3]
[1, 1]
[2, 3]
[3, 4]