输出数组中出现次数最多且值最大的数字----python

class Solution():
    #求最多的数
    def find_max(self,list):
        num = 0
        for i in list:
            print(i)
            if list.count(i) > num:
                num = list.count(i)
                value = i
        return value

    #求最多且最大的数
    def find_most_num(self,list):
        num = 0
        most = 0
        for i in list:
            print(i)
            if list.count(i) >= num:
                if i > most:
                    most = i
                    num = list.count(i)
        return most
    #另一种算法
    def find_most_dict(self,list):
        dict = {}
        for i in list:
            if i in dict.keys():
                dict[i] += 1
            else:
                dict[i] = 1
        print(dict)
        max = 0
        for k,v in dict.items():
            if v >= max:
                value = k
                max = v
                if k > value:
                    value = k
        return value




if __name__ =='__main__':
    list = [1,2,8,13,3,4,8,8,2,5,3,1,2,4,5,5,2,8,19,8,2]
    solution = Solution()
    # result = solution.find_max(list)
    # print('==============')
    # print(result)
    print('=====================================')
    #result2 = solution.find_most_num(list)
    print('==============')
    #print(result2)
    result3 = solution.find_most_dict(list)
    print('==============')
    print(result3)

 

posted @ 2020-03-10 22:22  图图图图胡图图  阅读(1395)  评论(0编辑  收藏  举报