Python压缩运行长度编码 [英] Python Compression Run Length encoding
问题描述
我试图学习游程编码,但我发现我无法做到这一在线挑战.它要求您编写一个称为compression(strg)的压缩函数,该函数将长度为64的二进制字符串strg作为输入,并返回另一个二进制字符串作为输出.输出的二进制字符串应为输入字符串的游程长度编码.
I am trying to learn about run length encoding and I found this challenge online that I cant do. It requires you to write a compression function called compression(strg) that takes a binary string strg of length 64 as input and returns another binary string as output. The output binary string should be a run-length encoding of the input string.
压缩('1010101001010101101010100101010110101010010101011010101001010101')
compression('1010101001010101101010100101010110101010010101011010101001010101')
'1010101001010101 * 4'
'1010101001010101*4'
这里是我所拥有的,但是找不到这种模式:
Here is what I have, but this does NOT find the pattern:
from itertools import *
def compression(strg):
return [(len(list(group)),name) for name, group in groupby(strg)]
我需要一些帮助来解决这个问题.
I need some help solving this.
推荐答案
我相信您正在将RLE与Lempel/Ziv滑动窗口压缩相结合.
I believe that you are conflating RLE with Lempel/Ziv sliding window compression.
RLE严格适用于重复的字符: WWWWWWWW
=> W8
RLE strictly works on repeated characters: WWWWWWWW
=> W8
LZ 有一个滑动窗口,它将按照您的描述选择图案.
LZ has a sliding window that will pick up patterns as you describe.
David MacKay的网站中有使用Python的压缩示例代码,包括LZ
David MacKay's site has example compression codes in Python, including LZ
这篇关于Python压缩运行长度编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!