Python 该函数针对给定的相同问题返回不同的答案

Python 该函数针对给定的相同问题返回不同的答案,python,python-3.x,Python,Python 3.x,必需的是一个函数,它接受字符串并返回其中重复次数最多的字符,而不考虑标点符号、空格和数字,它还处理“a”==“a”,如果字符串具有相同的重复字符,则返回拉丁字母表中的第一个字母 下面是示例中给出的函数,我已经对其进行了注释,以获得更多说明 def checkio(text): # any char result = "a" # iterating through small chars for i in text.lower(): # iterat

必需的是一个函数,它接受字符串并返回其中重复次数最多的字符,而不考虑标点符号、空格和数字,它还处理
“a”==“a”
,如果字符串具有相同的重复字符,则返回拉丁字母表中的第一个字母

下面是示例中给出的函数,我已经对其进行了注释,以获得更多说明

def checkio(text):
    # any char
    result = "a"
    # iterating through small chars
    for i in text.lower():
        # iterating through lowercase letters only and not considering punctuation, white spaces and letters
    if i in string.ascii_lowercase:
        # If i is repeated more than the result
        if text.count(i) > text.count(result):
            result = i
        # in case the letters are equal in repeated the same time
        elif text.count(i) == text.count(result):
            # returning according to the letter which comes first in the 
              Latin alphabet
            if string.ascii_lowercase.find(i) < string.ascii_lowercase.find(result):
                result = i
    return result

print(checkio("Hello World!"))
print(checkio("How do you do?"))
print(checkio("One"))
print(checkio("Oops!"))
print(checkio("abe"))
print(checkio("a" * 9000 + "b" * 1000))

# Here is the problem
print(checkio("AAaooo!!!!")) # returns o
print(checkio("aaaooo!!!!")) # returns a --> the right solution!
def checkio(文本):
#任何字符
结果=“a”
#遍历小字符
对于text.lower()中的i:
#仅遍历小写字母,不考虑标点、空格和字母
如果i为string.ascii_小写:
#如果我重复的次数超过结果
如果text.count(i)>text.count(结果):
结果=i
#如果字母在同一时间重复相同
elif text.count(i)=text.count(结果):
#根据第一封信返回
拉丁字母
如果string.ascii_lowercase.find(i)正确的解决方案!

您对
文本的调用。计数
不要先调用
lower()
。在函数顶部,应该调用
text=text.lower()
。然后您的
text.count
调用将使用与迭代器相同的标准化小写字符。

何时执行text.count(i)对于“AAaooo!!!!”,它执行以下操作:A:count=2 A:count=1 o:count=3。。。。等等

因此,在计算字符之前,需要将整个字符串转换为小写。那会解决你的问题

def checkio(text):
    # any char
    result = "a"
    # iterating through small chars
    for i in text.lower():
        # iterating through lowercase letters only and not considering punctuation, white spaces and letters
      if i in string.ascii_lowercase:
          # If i is repeated more than the result
          if text.lower().count(i) > text.lower().count(result):
              result = i
          # in case the letters are equal in repeated the same time
          elif text.lower().count(i) == text.lower().count(result):
              # returning according to the letter which comes first in the Latin alphabet
              if string.ascii_lowercase.find(i) < string.ascii_lowercase.find(result):
                  result = i
    return result


print(checkio("AAaooo!!!!")) # returns a
print(checkio("aaaooo!!!!")) # returns a 
def checkio(文本):
#任何字符
结果=“a”
#遍历小字符
对于text.lower()中的i:
#仅遍历小写字母,不考虑标点、空格和字母
如果i为string.ascii_小写:
#如果我重复的次数超过结果
如果text.lower().count(i)>text.lower().count(结果):
结果=i
#如果字母在同一时间重复相同
elif text.lower().count(i)=text.lower().count(结果):
#根据拉丁字母表中第一个字母返回
如果string.ascii_lowercase.find(i)
对于
.count()
您只在小写字符串上循环。您仍然在使用原始字符串。您的代码中第一次
for
之后没有缩进问题吗?缩进问题只是因为我在这里写错了,但在我的编辑器中没有问题!那么,如果我必须再次调用.lower(),那么从一开始就遍历小写有什么好处呢?它们已经是小写了!!调用text.lower()不会更新“text”。为了能够只调用lower()方法一次,您应该将结果保存到其他变量,如“new_text=text.lower()”。然后,您就可以对新的_文本执行所有操作,而无需反复调用lower()方法。谢谢,这很有帮助。