我在中有带相同字符块的字符串,例如“1254,,,,,,,,,,,,,,,,982”我的目标是用1254(,16)982行的内容替换它,这样就可以重建原始字符串。如果有人能给我指出正确的方向,我将不胜感激

最佳答案

您正在寻找run-length encoding:这里是一个基于this one的Python实现。

import itertools

def runlength_enc(s):
    '''Return a run-length encoded version of the string'''
    enc = ((x, sum(1 for _ in gp)) for x, gp in itertools.groupby(s))
    removed_1s = [((c, n) if n > 1 else c) for c, n in enc]
    joined = [["".join(g)] if n == 1 else list(g)
                    for n, g in itertools.groupby(removed_1s, key=len)]
    return list(itertools.chain(*joined))

def runlength_decode(enc):
    return "".join((c[0] * c[1] if len(c) == 2 else c) for c in enc)

例如:
print runlength_enc("1254,,,,,,,,,,,,,,,,982")
# ['1254', (',', 16), '982']
print runlength_decode(runlength_enc("1254,,,,,,,,,,,,,,,,982"))
# 1254,,,,,,,,,,,,,,,,982

(请注意,只有在字符串中有很长的运行时,这才有效)。

10-08 07:12