开发者

heapify returns NoneType

开发者 https://www.devze.com 2023-04-05 13:50 出处:网络
I have b = heapify([5,4,9,1]) and if I do a开发者_运维技巧 type(b) It says it\'s NoneType instead of list type,anyone know what I\'m doing wrong??The heapify() method transforms the list in-pla

I have

b = heapify([5,4,9,1])

and if I do a开发者_运维技巧

type(b)

It says it's NoneType instead of list type, anyone know what I'm doing wrong??


The heapify() method transforms the list in-place. This means that it alters the list, but does not returned the modified list. As agf mentions below, heapify() returns None to protect you from this mistake. Therefore, if you do

lst = [5,4,9,1]
heapify(lst)
type(lst)

you will see that lst is now heapified. See the library reference for more info.


heapify mutates the list passed to it; just like how l.sort() does.

>>> import heapq
>>> l = [9, 8, 7, 6]
>>> heapq.heapify(l)
>>> l
[6, 8, 7, 9]
0

精彩评论

暂无评论...
验证码 换一张
取 消