以下是两者的压缩和解压缩:def compress(uncompressed):

"""Compress a string to a list of output symbols."""

# Build the dictionary.

dict_size = 256

dictionary = dict((chr(i), chr(i)) for i in xrange(dict_size))

# in Python 3: dictionary = {chr(i): chr(i) for i in range(dict_size)}

w = ""

result = []

for c in uncompressed:

wc = w + c

if wc in dictionary:

w = wc

else:

result.append(dictionary[w])

# Add wc to the dictionary.

dictionary[wc] = dict_size

dict_size += 1

w = c

# Output the code for w.

if w:

result.append(dictionary[w])

return result

def decompress(compressed):

"""Decompress a list of output ks to a string."""

from cStringIO import StringIO

# Build the dictionary.

dict_size = 256

dictionary = dict((chr(i), chr(i)) for i in xrange(dict_size))

# in Python 3: dictionary = {chr(i): chr(i) for i in range(dict_size)}

# use StringIO, otherwise this becomes O(N^2)

# due to string concatenation in a loop

result = StringIO()

w = compressed.pop(0)

result.write(w)

for k in compressed:

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] = w + entry[0]

dict_size += 1

w = entry

return result.getvalue()

# How to use:

compressed = compress('TOBEORNOTTOBEORTOBEORNOT')

print (compressed)

decompressed = decompress(compressed)

print (decompressed)

希望这有帮助

python压缩算法_用python实现LZ78压缩算法相关推荐

  1. 第一章 第一节:Python基础_认识Python

    Python基础入门(全套保姆级教程) 第一章 第一节:Python基础_认识Python 1. 什么是编程 通俗易懂,编程就是用代码编写程序,编写程序有很多种办法,像c语言,javaPython语言 ...

  2. java python算法_用Python,Java和C ++示例解释的排序算法

    java python算法 什么是排序算法? (What is a Sorting Algorithm?) Sorting algorithms are a set of instructions t ...

  3. excel python插件_利用 Python 插件 xlwings 读写 Excel

    Python 通过 xlwings 读取 Excel 数据 去年底公司让我做设备管理,多次委婉拒绝,最终还是做了.其实我比较喜欢技术.做管理后发现现场没有停机率统计,而原始数据有,每次要自己在Exce ...

  4. 网络安全用python吗_使用Python进行网络安全渗透——密码攻击测试器

    相关文章: 本篇将会涉及: HTTP 基本认证 对HTTP Basic认证进行密码暴力攻击测试 什么是HTTP 基本认证 HTTP基本认证(HTTP Basic Authentication)是HTT ...

  5. 动态照片墙 python 实现_利用python生成照片墙的示例代码

    这篇文章主要介绍了利用python生成照片墙的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 PIL(Python Im ...

  6. python字符串_(Python基础教程之七)Python字符串操作

    Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...

  7. python 字符识别_使用python进行光学字符识别入门

    python 字符识别 语言模型设计 (Language Model Designing) Optical Character Recognition is the conversion of 2-D ...

  8. 类的继承python事例_【Python五篇慢慢弹(5)】类的继承案例解析,python相关知识延伸...

    作者:白宁超 2016年10月10日22:36:57 摘要:继一文之后,笔者又将python官方文档认真学习下.官方给出的pythondoc入门资料包含了基本要点.本文是对文档常用核心要点进行梳理,简 ...

  9. 【100天精通python】Day1:python入门_初识python,搭建python环境,运行第一个python小程序

    目录 专栏导读 1 初始python python 概述 python的应用领域 应用python的公司 2 搭建python 开发环境 2.1 安装python(以windows 系统为例)(1)下 ...

  10. python计算机_基础python计算机知识

    1.计算机基础知识 计算机基础 :组成---输入输出设备 储存器 CPU 内存 cpu 中央处理器 :处理各种数据的 内存 存储数据 硬盘 存储数据的 什么是操作系统:控制计算机的工作流程 软件 什么 ...

最新文章

  1. 关于用Delphi开发的一些基本的套路
  2. (4.7)mysql备份还原——深入解析二进制日志(3)binlog的三种日志记录模式详解...
  3. 如何使用系统自带的日志转储功能logroate.存放应用日志
  4. 自然语言处理语言资源项目
  5. python安装matlab库_[python][matlab]在python36上安装matlab2015b引擎
  6. gradle 修改java代码_自定义一个gradle插件动态修改jar包Class文件
  7. pytorch 中nn.MaxPool1d() 和nn.MaxPool2d()对比
  8. Capture One如何创建和应用样式和预设?
  9. 【日常折腾】Y7000P拆机-更换触控板
  10. ass字幕转换成文本文件
  11. Palabos User Guide中文解读 | 第十四章 | 网格加密Refinement
  12. 《经营的本质》1-“经营的基本元素”读后感及读书笔记
  13. 家喻户晓的足力健,凭什么征服中老年人?
  14. java.sql.SQLException: Incorrect string value: ‘\xE4\xB8\x8A\xE6\xB5\xB7‘ for column ‘xxx‘
  15. Lora技术- 码片/码元/数据速率以及空中时间计算
  16. 音符起始点检测(音频节奏检测)(5)
  17. 求qq微信快捷回复软件-微快聊聊天助手小程序
  18. 如何利用CCleaner快速查找重复文件?
  19. Atitit.团队文化建设------影响组织的的一些原理 法则 定理 效应 p826.v4
  20. CAD梦想画图中的“线型设置”

热门文章

  1. session和cookie的区别 session失效
  2. python编程(orm原理和实践)
  3. 提高代码的运行效率(1)
  4. linux查看jar包内容命令,【达内Java教程】用linux查看jar包内文件命令
  5. rem单位中html默认字号,轻松掌握CSS3中的字体大小单位rem的使用方法
  6. 出租广告Java代码_Spring cloud 查询返回广告创意实例代码
  7. Java中六种List集合循环遍历取值
  8. Java基本数据包装类_java基本数据类型的包装类
  9. 多项式拟合缺点_拟合多项式的最小二乘法
  10. ctfshow-WEB-web9( MD5加密漏洞绕过)