如何修复 TypeError: unhashable type: 'dict'

时间:2021-04-18 10:34:14

标签: python dictionary compression

**解压函数:从压缩函数中加载一个文件bin **

def lzwDecompressionText () :
loadCompressed = open ("CompreText.bin","rb")
compressed = pickle.load(loadCompressed)    
print("", compressed)
dictionary =defaultdict(dict)
lenDictionary = 256
decompressed_data = ""
string=""
for i in range(256):
    dictionary[i] = chr(i)
Traceback (most recent call last):
  File "c:/Users/anton/Documents/GitHub/Project-LZW/LZW-Project/test.py", line 21, in <module>
    lzwDecompressionText()
  File "c:\Users\anton\Documents\GitHub\Project-LZW\LZW-Project\textLzw.py", line 70, in lzwDecompressionText
    if not (code in dictionary):
TypeError: unhashable type: 'dict'
for code in compressed:
    if not (code in dictionary):
        dictionary[code] = string + (string[0])
decompressed_data += dictionary[code]
if not(len(string) == 0):
    dictionary[lenDictionary] = string + (dictionary[code][0])
    lenDictionary += 1
string = dictionary[code]

1 个答案:

答案 0 :(得分:0)

似乎您在 code 中的至少一些 compressed 值(从腌制文件加载)恰好是 dict 类型,这与您对 {{1 }}。

另见:TypeError : Unhashable type