1

我正在尝试制作一个霍夫曼树程序,以便将给定的哈希转换为霍夫曼树。我想返回一个元组列表,每个元组都有子元素、频率、父元素和赋值 0 0r 1。

但是当我运行代码时,它显示了太多需要解包的值。你能验证我的代码吗?

temp_var = -1

new_heap = []
stack_heap = []
ref_hash = freq_table.copy()
len_freqtable = len(freq_table)
while(len_freqtable > 1):

    new_heap.append(pop(freq_table))
    new_heap.append(pop(freq_table))
    pop_first = new_heap.pop(0)
    freq_first = ref_hash[pop_first]
    pop_second = new_heap.pop(0)
    freq_second = ref_hash[pop_second]
    append_composite = pop_first + pop_second
    stack_heap.append((pop_first,freq_first,append_composite,'0'))

    stack_heap.append((pop_second,freq_second,append_composite,'1'))
    freq_table[append_composite] = freq_first + freq_second
    ref_hash[append_composite] = freq_first + freq_second
    len_freqtable = len_freqtable - 1
    temp_var = temp_var - 1 

return stack_heap
4

1 回答 1

4

你肯定会做这样的事情

def myFunction()
    return (1,1,1)

a,b = myFunction()   #raises an 'Too many values to unpack' Error

a,b,c = myFunction() #this works

a = myFunction() # this works too, a is now a tuple

检查函数的所有返回值,看看返回的元素数量是否符合预期。

于 2013-11-12T12:06:44.100 回答