例如apple中出现最多的是p,将其修改为c
方法一
import string
class Solution:
def getMostWord(self,wordstr):
#将字符串小写化
wordstr = wordstr.lower()
#string.ascii_lowercase表示字母串'abcdef··z'
return max(string.ascii_lowercase, key=wordstr.count)
handler=Solution()
re=handler.getMostWord("HOW,。、;ldfosudfjnxchvJJJvdjjjjdlsllhh")
print(re)
方法二
def wanted(text):
text=text.lower() #全部改为小写
target_letter = ''
target_count = 0
for i in range(len(text)):
count = text.count(text[i])
if not text[i].isalpha() or target_letter == text[i]:
# 避免重复判断
continue
if count == target_count:
# 比较字母表顺序
orders = [text[i], target_letter]
orders.sort()
target_letter = orders[0]
elif count > target_count:
target_count = count
target_letter = text[i]
return [target_letter, target_count]
res = wanted('iiiIIIIoJJDL,。、SPPNNs')
print(res)