Posted By

rishi_devan on 09/10/15


Tagged


Versions (?)

LZW Compression


 / Published in: Python
 

Lempel Ziv Welch Compression Algorithm in python

  1. string = "thisisthe"
  2. dictionary = {chr(i):i for i in range(97,123)}
  3.  
  4. last = 256
  5. p = ""
  6. result = []
  7.  
  8. for c in string:
  9. pc = p+c
  10. if pc in dictionary:
  11. p = pc
  12. else:
  13. result.append(dictionary[p])
  14. dictionary[pc] = last
  15. last += 1
  16. p = c
  17.  
  18. if p != '':
  19. result.append(dictionary[p])
  20.  
  21. print(result)

Report this snippet  

You need to login to post a comment.