Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: '<' not supported between instances of 'tuple' and 'str'

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 list

but 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().

like image 622
rhazkoomar Avatar asked Jan 28 '17 18:01

rhazkoomar


People also ask

How do I fix TypeError not supported between instances of tuple and int?

The Python "TypeError: '>' not supported between instances of 'tuple' and 'int'" occurs when we use a comparison operator between values of type tuple and int . To solve the error, access the tuple at a specific index or compare the tuple's length to an integer.

What is tuple in Python?

Tuple. Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable.

Is a tuple an object?

A tuple is a collection of objects which ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.


Video Answer


1 Answers

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?

like image 174
Martijn Pieters Avatar answered Oct 27 '22 06:10

Martijn Pieters