返回字符串中出现最多的字符

题目:
'''
You are given a text, which contains different english letters and punctuation symbols. You should find the most frequent letter in the text. The letter returned must be in lower case.
While checking for the most wanted letter, casing does not matter, so for the purpose of your search, "A" == "a". Make sure you do not count punctuation symbols, digits and whitespaces, only letters.

If you have two or more letters with the same frequency, then return the letter which comes first in the latin alphabet. For example -- "one" contains "o", "n", "e" only once for each, thus we choose "e".
'''
方法一:
def checkio(text):

    #replace this for solution
    text = text.lower()
    key1 = text.count
    '''
    string.ascii_lowercase 等价于 ‘abcdefghijklmnopqrstuvwxyz‘ 
    而max()函数key参数的作用是:筛选符合key函数的返回值的最大值,如果有多个符合条件的值,则选取第一个。
    '''
    return max(string.ascii_lowercase,key = key1)

    方法二:

  

def checkio(text):
    count = Counter([x for x in text.lower() if x.isalpha()])
    m = max(count.values())
    return sorted([x for (x, y) in count.items() if y == m])[0]

 

posted @ 2018-03-30 09:57  蓝冰lanbing  阅读(216)  评论(0编辑  收藏  举报