计算字母在python中连续出现的次数

时间:2018-06-07 19:32:01

标签: python python-3.x run-length-encoding

当连续出现相同的字符时,如何计算它连续出现的次数?

例如AAARRRGGHH并输出如下结果 A 3 R 3 G 2 H 2

1 个答案:

答案 0 :(得分:0)

您尝试实现的目标称为run-length-encoding

你可以通过简单地迭代文本和计算字符,将它们存储在元组中并从这些元组创建输出来解决这个问题:

text = "AAARRRGGHH"

def runLengthEncode(t):
    cnt = 0       # how often did we see this character?
    cha = t[0]    # which character
    rv = []       # resulting list that stores our (character, count) tuples
    for c in t:   # process each character of the given text
        if c == cha:  # if it is equal to what we look for
            cnt +=1       # increment counter
        else:
            rv.append( (cha,cnt) )
            cnt = 1       # append so far counted ones as tuple (character,count)
            cha = c       # and remember the new char with a count of 1

    rv.append( (cha,cnt) ) # add the last character and its count

    # produce the output from our remembered tuples 
    return ' '.join( "{} {}".format(charac, nr) for charac,nr in rv)

print( runLengthEncode(text) )

输出:

A 3 R 3 G 2 H 2

问题answer by Martijn Pieters可以找到Run Length Encoding in Python with List Comprehension更多的pythonic(itertools)答案:

(有点编辑)

from itertools import groupby

string = "AAARRRGGHH"
print(' '.join(['{} {}'.format(k, sum(1 for _ in g)) for k, g in groupby(string)]))

输出:

 A 3 R 3 G 2 H 2