def decompress(data, *args, **kwargs):
from cStringIO import StringIO
data = list(data)
dict_size = 256
dictionary = dict((i, chr(i)) for i in xrange(dict_size))
result = StringIO()
w = chr(data.pop(0))
result.write(w)
for k in data:
if k in dictionary:
entry = dictionary[k]
elif k == dict_size:
entry = w + w[0]
else:
raise ValueError('Bad compressed k: %s' % k)
result.write(entry)
# Add w+entry[0] to the dictionary
dictionary[dict_size] = str(w + entry[0])
dict_size += 1
w = entry
return result.getvalue()
评论列表
文章目录