找出字符串s="aaabbbccceeefff111144444"中,字符出现次数最多的字符

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"中,字符出现次数最多的字符)