Python LZW 算法()
- 技术交流
- 2024-10-25 23:29:02
Python LZW 算法()
Python LZW 算法LZW 压缩算法string = "thisisthe"dictionary = {chr(i):i for i in range(97,123)} last = 256p = ""result = [] for c in string: pc = p+c if pc in dictionary: p = pc else: result.append(dictionary[p]) dictionary[pc] = last last += 1 p = c if p != '': result.append(dictionary[p]) print(result)
以上代码运行结果为:
[116, 104, 105, 115, 258, 256, 101]LZW 解压缩算法dictionary = {i:chr(i) for i in range(97,123)}last = 256arr = [97, 97, 98, 256, 258, 257, 259] result = []p = arr.pop(0)result.append(dictionary[p]) for c in arr: if c in dictionary: entry = dictionary[c] result.append(entry) dictionary[last] = dictionary[p] + entry[0] last += 1 p = c print(''.join(result))以上代码运行结果为:
aabaabaabaabPython LZW 算法()由讯客互联技术交流栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“Python LZW 算法()”