heap - Does Python's heapify() not play well with list comprehension and slicing? -


I found an interesting bug in a program that I had somewhat highlighted, and I wonder if I correct it I understand the short version is that which does not actually order a list, it just shocks the list in a pile-centric way. In particular, I was expecting heapify () as a result in an order list, which used to help ease the list of fashion respectively.

Using the preference Q example, as in Python documentation:

  Happie, Hupp, Happp from Random Import Shuffle Class Item (object): def __init __ (self, name): self.name = name lst = [] # A random number of unique numbers for category i (class (100), 15): this = item ("% i"% i Hippus (lst, (i, it)) results in print ([i [0] for < 

  & Gt; & Gt; & Gt; [2, 22, 7, 69, 32, 40, 10, 97, 89, 33, 45, 51, 94, 27, 67]  

This, we note, this is not The order of the original list, but apparently in the form of some heap-centric order. I was expecting to order completely.

In order to run the list through the results of hyphepe () in the form of a test, no change (as the list has already been ordered in a pile-wise order):

Havish (LST) print (for [i [0] in)) gt; & Gt; & Gt; [2, 22, 7, 69, 32, 40, 10, 97, 89, 33, 45, 51, 94, 27, 67]

Hopp () function expected in order:

  lst2 = [] while lst: lst2.append (heappop (lst)) print (for [i [0]. In lst2]) gt; & Gt; & Gt; [2, 7, 10, 22, 27, 32, 33, 40, 45, 51, 67, 69, 89, 94, 97]  

Therefore, it seems that enjoy () and heappop () functions are enabled / P>

The result: Any slicing action and list evaluation process on a redirected list will get results of non-order.

Is this true, and is it always true?

(BTW: Python 3.0.1 on a WinXP system)

So yes, you are right, if you expect To make an indexed list behave as sorted as a sorted list, you will be disappointed. You can only do a heap of thought about a heap that is heaps [0] always its The smallest element is.

(It is difficult to add a lot to what you have already written - your question is how things are excellent writing. 8 -)


Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -