小编rha*_*mar的帖子

TypeError:'tuple'和'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
Run Code Online (Sandbox Code Playgroud)

但是我用以下参数提供函数:

[(1, 'b'), (1, 'd'), (1, 'g'), (2, 'c'), (2, 'f'), (3, 'a'), (5, 'e')] …
Run Code Online (Sandbox Code Playgroud)

python sorting python-3.x

8
推荐指数
1
解决办法
3万
查看次数

标签 统计

python ×1

python-3.x ×1

sorting ×1