LZW Compression


/ Published in: Python
Save to your folder(s)

Lempel Ziv Welch Compression Algorithm in python


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.