heapq [优先队列算法]

    from heapq import *
    h = []
    heappush(h, (5, 'write code'))          # 放入队列
    heappush(h, (7, 'release product'))
    heappush(h, (1, 'write spec'))
    heappush(h, (3, 'create tests'))
    heappop(h)                              # 从队列取出 第一次是1

    from heapq import *
    def heapsort(iterable):
        h = []
        for value in iterable:
            heappush(h, value)
        return [heappop(h) for i in range(len(h))]

    heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
文档更新时间: 2018-11-21 17:37   作者:RuM