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
Post a Comment