from collections import Counter
import re
def checkio(text: str) -> str:
text1 = re.findall('[^\W]', text.lower())
a = (''.join(text1))
text2 = re.findall('[a-z]', a)
a1 = (''.join(text2))
return(Counter(a1).most_common(1)[0][0])
print(checkio("Hello World!")) # == "l", "Hello test"
print(checkio("How do you do?")) # == "o", "O is most wanted"
print(checkio("One")) # == "e", "All letter only once."
print(checkio("Oops!")) # == "o", "Don't forget about lower case."
print(checkio("AAaooo!!!!")) # == "a", "Only letters."
print(checkio("abe")) # == "a", "The First."
print(checkio("a" * 9000 + "b" * 1000)) # == "a", "Long."
如果“One”应该是“e”,而“abe”应该是“a”,如何解决这个问题?
这里问了同样的问题:https ://ru.stackoverflow.com/questions/1036053/Most-frequent-letter-in-text
据我了解,您不仅需要按频率对结果进行排序,还需要按字母表中哪个字母排在第一位:
那么结果就是你想要的:
PS 而是不能排序,直接取最小值,这样更优一点。但也使用相同的键: