zoukankan      html  css  js  c++  java
  • 返回字符串中出现最多的字符

    题目:
    '''
    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]
  • 相关阅读:
    Execution Contexts (执行上下文)
    OOP—ECMAScript实现详解
    requireJS入门
    SqlServer 傲娇的表变量
    CSharp进阶 引用类型引发的血案
    CSharp进阶 都是请求惹的祸
    z-index问题
    js中事件(自定义事件)
    做了个后末日朋克风的梦
    昨晚的梦
  • 原文地址:https://www.cnblogs.com/lanbing/p/8674577.html
Copyright © 2011-2022 走看看