📜  Python中的运行长度编码

📅  最后修改于: 2022-05-13 01:55:00.354000             🧑  作者: Mango

Python中的运行长度编码

给定一个输入字符串,编写一个函数,返回输入字符串。

例如,如果输入字符串是“wwwwaaadexxxxxx”,那么函数应该返回“w4a3d1e1x6”。

例子:

Input  :  str = 'wwwwaaadexxxxxx'
Output : 'w4a3d1e1x6'

此问题已有解决方案,请参阅运行长度编码链接。在这里,我们将使用 OrderedDict 在Python中快速解决这个问题。方法很简单,首先我们创建一个有序字典,其中包含输入字符串的字符作为键,0 作为它们的默认值,现在我们运行一个循环来计算每个字符的频率并将其映射到它对应的键。

# Python code for run length encoding
from collections import OrderedDict
def runLengthEncoding(input):
  
    # Generate ordered dictionary of all lower
    # case alphabets, its output will be 
    # dict = {'w':0, 'a':0, 'd':0, 'e':0, 'x':0}
    dict=OrderedDict.fromkeys(input, 0)
  
    # Now iterate through input string to calculate 
    # frequency of each character, its output will be 
    # dict = {'w':4,'a':3,'d':1,'e':1,'x':6}
    for ch in input:
        dict[ch] += 1
  
    # now iterate through dictionary to make 
    # output string from (key,value) pairs
    output = ''
    for key,value in dict.items():
         output = output + key + str(value)
    return output
   
# Driver function
if __name__ == "__main__":
    input="wwwwaaadexxxxxx"
    print (runLengthEncoding(input))

输出:

'w4a3d1e1x6'


另一个代码:

def encode(message):
    encoded_message = ""
    i = 0
   
    while (i <= len(message)-1):
        count = 1
        ch = message[i]
        j = i
        while (j < len(message)-1):
            if (message[j] == message[j+1]):
                count = count+1
                j = j+1
            else:
                break
        encoded_message=encoded_message+str(count)+ch
        i = j+1
    return encoded_message
  
#Provide different values for message and test your program
encoded_message=encode("ABBBBCCCCCCCCAB")
print(encoded_message)