#coding: utf-8
#LZW压缩解压算法
def myzip(ss):
ss=ss+'#'
dict0={'a':1,'b':2,'c':3}
s=ss[0]
for c in ss[1:]:
if s+c in dict0:
s=s+c
else:
yield dict0[s]
dict0[s+c]=len(dict0)+1
s=c
def myunzip(nn):
dict1={1:'a',2:'b',3:'c'}
s=dict1[nn[0]]
yield s
for i in nn[1:]:
if i in dict1:
yield dict1[i]
dict1[len(dict1)+1]=s+dict1[i][0]
else:
print '$'
dict1[len(dict1)+1]=s+s[0]
yield s+s[0]
s=dict1[i]
sss='abcabcaaabbaacbbacabaaaaaabaaabcccacaaaaacccbaaacaaabbbaaaacaaabbbbb'
out1=[i for i in myzip(sss)]
out2=''.join([i for i in myunzip(out1)])
print '---', sss
print '~~~', out1
print '^^^', out2
|