python huffman coding Exception Unorderable Types -


i trying write huffman coding in python 3 code http://en.literateprograms.org/huffman_coding_%28python%29 doesn't work. if run code in python 2.7, works well.

the following lines problem:

heapq.heapify(trees) while len(trees) > 1:     childr, childl = heapq.heappop(trees), heapq.heappop(trees)     parent = (childl[0] + childr[0], childl, childr)     heapq.heappush(trees, parent) 

i typeerror in heapq.heappush(u,parent): "unorderable types: tuple() < str()"

so i've searched solution, , think, have implement _lt _ function. possible 2 or more nodes have same frequency, heapq tries compare tuples , think, can't compare tuple of tuple. dont know , how have create compare-method solve problem? can help? ;-)

the reason in python3x can't compare items of 2 different type:

>>> "foo" < 1 traceback (most recent call last):   file "<ipython-input-5-de2fb49cc8c4>", line 1, in <module>     "foo" < 1 typeerror: unorderable types: str() < int() 

Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

How to call a javascript function after the page loads with a chrome extension? -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -