TypeError: '<' not supported between instances of 'tuple' and 'str'
Sebastian Wright
I have a method that build huffman tree which is as follows:
def buildTree(tuples) : while len(tuples) > 1 : leastTwo = tuple(tuples[0:2]) # get the 2 to combine theRest = tuples[2:] # all the others combFreq = leastTwo[0][0] + leastTwo[1][0] #enter code here the branch points freq tuples = theRest + [(combFreq,leastTwo)] # add branch point to the end tuples.sort() # sort it into place return tuples[0] # Return the single tree inside the listbut while I feed the function with following parameter:
[(1, 'b'), (1, 'd'), (1, 'g'), (2, 'c'), (2, 'f'), (3, 'a'), (5, 'e')]I get the error as
File "<stdin>", line 7, in buildTree tuples.sort()
TypeError: '<' not supported between instances of 'tuple' and 'str'While debugging I found the error was in tuples.sort().
1 Answer
The error is thrown because you are creating inner nodes in (priority, (node, node)) form. For equal priorities, Python then tries to compare a symbol from a leaf node (so the second element in a (priority, symbol) node tuple) with the (node, node) tuple from an inner node:
>>> inner = (combFreq, leastTwo)
>>> inner
(2, ((1, 'b'), (1, 'd')))
>>> theRest[1]
(2, 'c')
>>> theRest[1] < inner
Traceback (most recent call last): File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'str' and 'tuple'For building a huffman tree, if you want to sort your array of nodes, you only really need to sort on the priority, ignoring the rest of the tuples (symbols or child nodes):
tuples.sort(key=lambda t: t[0])With that correction, your buildTree() function produces a tree:
>>> buildTree([(1, 'b'), (1, 'd'), (1, 'g'), (2, 'c'), (2, 'f'), (3, 'a'), (5, 'e')])
(15, ((6, ((3, 'a'), (3, ((1, 'g'), (2, 'c'))))), (9, ((4, ((2, 'f'), (2, ((1, 'b'), (1, 'd'))))), (5, 'e')))))Personally, I'd use a priority queue instead, avoiding sorting each time. See How to implement Priority Queues in Python?
6