繁体   English   中英

如何将代码修改为所需的格式?

[英]How can I modify my code for the desired format?

def rle_encode(data):
    encoding = ''
    prev_char = ''
    count = 1

    if not data: return ''

    for char in data:
        # If the prev and current characters
        # don't match...
        if char != prev_char:
            # ...then add the count and character
            # to our encoding
            if prev_char:
                encoding += str(count) + prev_char
            count = 1
            prev_char = char
        else:
            # Or increment our counter
            # if the characters do match
            count += 1
    else:
        # Finish off the encoding
        encoding += str(count) + prev_char
        return encoding

encoded_val = rle_encode(“aaaaBBbbCooa”)
print(encoded_val)

对于上面的字符串,我得到 output 为:“4a2B2b1C2o1a”

但预期的 output 是:“4a2B2bC2oa”

这意味着对于单个字符,我不需要输入 ist 编号,只需输入字符即可。

如果有人告诉我在哪里更改代码以获得 output 的预期格式,那就太好了。

您可以通过使用itertools.groupby来实现。 如果组的长度大于 1,则添加计数,否则只需添加字符本身。

from itertools import groupby

def rle_encode(data):
    out = ''
    for key, group in groupby(data):
        run = len(list(group))
        if run > 1:
            out += str(run) + key
        else:
            out += key
    return out

例子

>>> rle_encode("aaaaBBbbCooa")
'4a2B2bC2oa'

这是您的固定代码。

def rle_encode(data):
    encoding = ''
    prev_char = ''
    count = 1

    if not data: return ''

    for char in data:
        # If the prev and current characters
        # don't match...
        if char != prev_char:
            # ...then add the count and character
            # to our encoding
            if prev_char:
                if count > 1:
                    encoding += str(count) + prev_char
                else:
                    encoding += prev_char
            count = 1
            prev_char = char
        else:
            # Or increment our counter
            # if the characters do match
            count += 1
    else:
        # Finish off the encoding
        if count > 1:
            encoding += str(count) + prev_char
        else:
            encoding += prev_char
        return encoding

显然,@Cory 的解决方案更好,它甚至大大简化了您的 function。 但是如果你仍然想要你的解决方案,我认为这就像删除'1's一样简单

def rle_encode(data):
    encoding = ''
    prev_char = ''
    count = 1

    if not data: return ''

    for char in data:
        # If the prev and current characters
        # don't match...
        if char != prev_char:
            # ...then add the count and character
            # to our encoding
            if prev_char:
                encoding += str(count) + prev_char
            count = 1
            prev_char = char
        else:
            # Or increment our counter
            # if the characters do match
            count += 1
    # Finish off the encoding
    encoding += str(count) + prev_char
    return encoding.replace('1', '')

encoded_val = rle_encode('aaaaBBbbCooa')
print(encoded_val)

顺便说一句,我认为您的最后一个“其他”是不必要的。

    def rle_encode(data):
    encoding = ''
    prev_char = ''
    count = 1

    if not data: return ''

    for char in data:
        # If the prev and current characters
        # don't match...
        print(char," ", count)
        if char != prev_char:
            # ...then add the count and character
            # to our encoding
            if prev_char:
                if count != 1:
                    encoding += str(count) + prev_char
                else:
                    encoding += prev_char

            count = 1
            prev_char = char
        else:
            # Or increment our counter
            # if the characters do match
            count += 1
    else:
        # Finish off the encoding
        if count != 1:
            encoding += str(count) + prev_char
        else:
            encoding += prev_char
        return encoding

encoded_val = rle_encode('aaaaBBbbCooa')
print(encoded_val)


Hope this will help you.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM