首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python文本编码程序

python文本编码程序
EN

Stack Overflow用户
提问于 2018-06-12 09:49:19
回答 5查看 97关注 0票数 1

当输入一个文本到定义run_length_encoder时,重复的字母应该被压缩,例如,当输入aaabbac时,输出应该是'a','a',3,'b','b',2,'a','c‘,但是我的代码不是压缩的。

代码语言:javascript
复制
def run_length_encoder(string):
#def compress(string):

    res = []

    count = 1

    #Add in first character
    res.append(string[0])

    #Iterate through loop, skipping last one
    for i in range(len(string)-1):
        if(string[i] == string[i+1]):
            count+=1
            res.append(string[i+1])
        else:
            if(count > 1):
                #Ignore if no repeats
                res.append(count)
            res.append(string[i+1])
            count = 1
    #print last one
    if(count > 1):
        res.append(str(count))
    return res

例如,当输入abbbbaa时,输出应该是这个'a','b','b',4,'a','a',2而我得到的是'a','b','b',4,'a','a','2‘

EN

回答 5

Stack Overflow用户

发布于 2018-06-12 12:42:18

你也可以这样做:

代码语言:javascript
复制
def run_length_encoder(str_):
    compressedString = ''
    countConsecutive = 0
    strLen = len(str_)
    for i in range(strLen):
        countConsecutive += 1
        if i + 1 >= strLen or str_[i] != str_[i + 1]:
            compressedString += '' + str_[i] + str(countConsecutive)
            countConsecutive = 0

    return compressedString

sample = 'aaabbac'
result = list(run_length_encoder(sample))
print(result)
票数 1
EN

Stack Overflow用户

发布于 2018-06-12 13:01:04

Itertools爱你,希望你快乐:

代码语言:javascript
复制
from itertools import chain, groupby

def run_length_encoder(src):
    return list(
        # chain.from_iterable flattens the series of tuples we make inside the
        # loop into a single list.
        chain.from_iterable(
            # groupby returns an iterable (item, group) where group is an
            # iterable that yields a copy of `item` as many times as that item
            # appears consecutively in the input. Therefore, if you take the
            # length of `group`, you get the run length of `item`. This
            # whole expression then returns a series of (letter, count)
            # tuples.
            (letter, len(list(group))) for letter, group in groupby(src)
        )
    )


print(run_length_encoder("aajjjjiiiiohhkkkkkkkkhkkkk"))
票数 1
EN

Stack Overflow用户

发布于 2018-06-12 10:19:17

你的逻辑需要修正。修复了处理偶数和奇数结束情况的编辑。

代码语言:javascript
复制
def run_length_encoder(string):
#def compress(string):

    res = []
    count = 1
    if(len(string) == 1):
        res.append(string[0])
        res.append(count)
        return res
    else:
        current = string[0]

        for i in range(1, len(string)):

            if(string[i] == current):
                count+=1
            else:
                res.append(current)
                res.append(count)
                current = string[i]
                count = 1
            i+=1
            if(i == len(string)):
                res.append(current)
                res.append(count)
        return res

字符串测试: string = "aaabbaadddaad“输出:'a',3,'b',2,'a',2,'d',3,'a',2,'d',1 string = "aaabbaaddd”输出:'a',3,'b',2,'a',2,'d',3 string = "aabccdd“输出:'a',2,'b',1,'c',2,'d',2

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50808205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档