小编Arp*_*ole的帖子

使用 heapq 时,heappush() 和 heapify() 对于 python 中的相同输入给出不同的输出,错误?

import heapq

heap = []   # creates an empty heap
item = [20, 4, 8, 10, 5, 7, 6, 2, 9]
for i in item:
    heapq.heappush(heap, i)  # pushes a new item on the heap

print('Heap obtained from heappush() : ', heap)

same_item = [20, 4, 8, 10, 5, 7, 6, 2, 9]
heapq.heapify(same_item)  # transforms list into a heap, in-place, in linear time

print('Heap obtained from heapify() : ', same_item)
Run Code Online (Sandbox Code Playgroud)

我的理解是heappush()heapify()应该给出相同的输出,而输出则相反。

Heap obtained from heappush() …
Run Code Online (Sandbox Code Playgroud)

python heap

4
推荐指数
1
解决办法
485
查看次数

标签 统计

heap ×1

python ×1