我需要使用包含ASCII和Huffman位之间的转换的文件来解码我用我的程序编码的霍夫曼代码.我已经在程序中从"代码"到ASCII这样的字典:
{'01110': '!', '01111': 'B', '10100': 'l', '10110': 'q', '10111': 'y'}
Run Code Online (Sandbox Code Playgroud)
我创建了这个函数:
def huffmanDecode (dictionary, text) :
Run Code Online (Sandbox Code Playgroud)
那需要字典和代码.我已经尝试在字典中搜索密钥并使用替换方法表单字符串和来自re的sub,但它们都没有正确地解码消息.例如,如果代码是:
011111011101110
Run Code Online (Sandbox Code Playgroud)
它应该直接解码为:
By!
Run Code Online (Sandbox Code Playgroud)
但我无法通过迭代代码和搜索字典中的匹配来做到这一点!
如何通过查找文本中的键并将其替换为值来使用字典中的键及其值来解码代码?
任何帮助是极大的赞赏.