美文网首页
找出字符串s="aaabbbccceeefff111144444

找出字符串s="aaabbbccceeefff111144444

作者: 钱从海推拿 | 来源:发表于2020-02-29 20:25 被阅读0次

    1、不考虑去重

    #不考虑去重
    s = "xssdddeeeeffff"
    max_times = 0
    result = {}
     
    for i in s:
        if s.count(i) > max_times:
            result.clear()
            result[i] = s.count(i)
            max_times = s.count(i)
        elif s.count(i) == max_times:
            result[i] = s.count(i)
     
    print (result)
    

    2、考虑去重

    str = 'a1fsfs111bbbcccccvvvvvnnnnboooooosssnb'
     
    class Countvalue():
        def countvalue(self, str1):
            '''
            利用set自身的去重功能
            :param str1: 对传进来的字符串过滤
            :return: 返回一个不含重复字符的list
            '''
            list1 = []
            result = []
            for x in str1:
                list1.append(x)
                result = set(list1)
            return result
        def count(self, str1):
            '''
            对已经去重的list进行遍历,因已去重
            从而减少循环次数,提高检索效率
            :param str1:
            :return:
            '''
            list = Countvalue().countvalue(str1)
            a = 0
            tump = {}
            for x in list:
                test = str1.count(x)
                if test > a:
                    tump.clear()
                    a = test
                    tump[x] = a
                elif test == a:
                    a = test
                    tump[x] = a
            return tump
     
    if __name__ == '__main__':
        print(Countvalue().count(str))
    
    

    相关文章

      网友评论

          本文标题:找出字符串s="aaabbbccceeefff111144444

          本文链接:https://www.haomeiwen.com/subject/gammhhtx.html