要解压缩的值过多。 python编程时出错

时间:2013-11-12 12:01:54

标签: python

我正在尝试制作一个霍夫曼树程序,以便将给定的哈希值转换为霍夫曼树。我想返回一个元组列表,每个元组都有子元素,它的频率,父元素和赋值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

1 个答案:

答案 0 :(得分: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

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