間でサポートされていない私は次の通りであるハフマンツリーを構築する方法がありますTypeError例外:「<」「タプル」のインスタンスと「STR」
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
を私は次のパラメータと機能を供給しつつ。
[(1, 'b'), (1, 'd'), (1, 'g'), (2, 'c'), (2, 'f'), (3, 'a'), (5, 'e')]
デバッグ、私はエラーがにあった見つけている間、私は
File "<stdin>", line 7, in buildTree
tuples.sort()
TypeError: '<' not supported between instances of 'tuple' and 'str'
としてエラーが出ます3210。
'leastTwo'はタプルのタプルで、' combFreq'(整数)で他の*タプルをラップします。その* inner *タプルと他のタプルのそれぞれの2番目の要素の文字列を比較することはできません。 –
つまり、 '(int、str)、(int、str))))')タプルではなく '(int、str)'タプルだけを追加することができます。 –
ここで、各タプルの**最初の**値をソートする場合は、そのタプルを抽出するソートキーを指定する必要があります。 –