太多的值无法解压缩。python编程时出错

2024-06-02 07:41:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: refnewlenstacktablehashpoptemp
1条回答
网友
1楼 · 发布于 2024-06-02 07:41:06

你肯定会做这样的事

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

检查函数的所有返回值,并查看返回的元素数是否如预期的那样。在

相关问题 更多 >