heapify returns NoneType
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]
精彩评论