【问题标题】:Finding consecutive letters in a list [closed]在列表中查找连续字母[关闭]
【发布时间】:2014-11-07 19:46:19
【问题描述】:

我有以下序列:'TATAAAAAAATGACA',我希望 Python 打印出包含最连续重复的字母......在这种情况下,这将是 7 'A's。仅使用foriflenrange 和一个计数变量,您将如何做到这一点?

【问题讨论】:

  • 这不是代码编写服务。您尝试过什么,到底有什么问题?

标签: python algorithm numbers


【解决方案1】:
def consecutiveLetters(word):
    currentMaxCount = 1
    maxChar = ''
    tempCount = 1

    for i in range(0, len(word) - 2):
        if(word[i] == word[i+1]):
            tempCount += 1
            if tempCount > currentMaxCount:
                currentMaxCount = tempCount
                maxChar = word[i]
        else:
            currentMaxCount = 1

    return [maxChar,tempCount]

result = consecutiveLetters("TATAAAAAAATGACA")

print("The max consecutive letter is ", result[0] , " that appears ", result[1], " times.")

这将打印:The max consecutive letter is A that appears 7 times.

【讨论】:

  • 对不起,我的困惑,我可以让我的代码也打印出'A',但我不能让它打印出“7”。不过感谢您的帮助,我很感激!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-08
  • 2020-03-20
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
  • 2020-12-07
  • 2013-04-25
相关资源
最近更新 更多