This question already has answers here:
Run length encoding in Python
(9个答案)
去年关门了。
我在寻找解决我问题的办法。我想制作一个程序,其中有人输入字符串,我把它覆盖成这样:
'ZZZZYYYZZ' -> 'Z4Y3Z2'

我愿意接受任何建议。
我的密码是:
def compress(s):
    e={}
    if s.isalpha():
        for i in s:
            if i in e:
                e[i] += 1
            else:
                e[i] = 1
    else:
        return None

    return ''.join(['{0}{1}'.format(k,v)for k,v in e.items()])

s=input("Write string: ")
compress(s)

这会产生错误的输出
Write string: ZZZZYYYZZ
Y3Z6

最佳答案

将未排序的数据分组成块是itertools.groupby的一项工作。

>>> from itertools import groupby
>>>
>>> s = 'ZZZZYYYZZ'
>>> ''.join('{}{}'.format(c, len(list(g))) for c, g in groupby(s))
'Z4Y3Z2'

关于groupby在这里生成的内容的Detrails:
>>> [(c, list(g)) for c, g in groupby(s)]
[('Z', ['Z', 'Z', 'Z', 'Z']), ('Y', ['Y', 'Y', 'Y']), ('Z', ['Z', 'Z'])]

~编辑~
没有中间列表的轻微内存优化:
>>> ''.join('{}{}'.format(c, sum(1 for _ in g)) for c, g in groupby(s))
'Z4Y3Z2'

~编辑2~
不是C1,我们能不能只有C?
>>> s = 'XYXYXXX'
>>> to_join = []
>>> groups = groupby(s)
>>>
>>> for char, group in groups:
...:    group_len = sum(1 for _ in  group)
...:    if group_len == 1:
...:        to_join.append(char)
...:    else:
...:        to_join.append('{}{}'.format(char, group_len))
...:
>>> ''.join(to_join)
'XYXYX3'

08-05 18:59