python 解压缩字符串_Python-压缩Ascii字符串

小编典典

使用压缩并不总是会减少字符串的长度!

考虑下面的代码;

import zlib

import bz2

def comptest(s):

print 'original length:', len(s)

print 'zlib compressed length:', len(zlib.compress(s))

print 'bz2 compressed length:', len(bz2.compress(s))

让我们在一个空字符串上尝试一下;

In [15]: comptest('')

original length: 0

zlib compressed length: 8

bz2 compressed length: 14

这样就zlib产生了额外的8个字符和bz214个字符。压缩方法通常在压缩数据前放置一个“标头”,以供解压缩程序使用。该头增加了输出的长度。

让我们测试一个单词;

In [16]: comptest('test')

original length: 4

zlib compressed length: 12

bz2 compressed length: 40

即使减去标题的长度,压缩也不会使单词变短。这是因为在这种情况下几乎没有压缩。字符串中的大多数字符仅出现一次。现在简短一句话;

In [17]: comptest('This is a compression test of a short sentence.')

original length: 47

zlib compressed length: 52

bz2 compressed length: 73

同样,压缩输出 大于 输入文本。由于文本的长度有限,因此重复很少,因此压缩效果不佳。

您需要相当长的文本块才能进行压缩,才能真正起作用。

In [22]: rings = '''

....: Three Rings for the Elven-kings under the sky,

....: Seven for the Dwarf-lords in their halls of stone,

....: Nine for Mortal Men doomed to die,

....: One for the Dark Lord on his dark throne

....: In the Land of Mordor where the Shadows lie.

....: One Ring to rule them all, One Ring to find them,

....: One Ring to bring them all and in the darkness bind them

....: In the Land of Mordor where the Shadows lie.'''

In [23]: comptest(rings)

original length: 410

zlib compressed length: 205

bz2 compressed length: 248

2020-07-28

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值