zoukankan      html  css  js  c++  java
  • 输出数组中出现次数最多且值最大的数字----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)
  • 相关阅读:
    influxdb 使用
    【刷题】如何查找最长链
    学习中的开源框架和系统
    常见错误总结
    开发者必备网站
    计算机基础知识以及常用业务场景
    (1)、hive框架搭建和架构简介
    hadoop安装和配置
    linux基础
    UML学习目录
  • 原文地址:https://www.cnblogs.com/turningli/p/12459250.html
Copyright © 2011-2022 走看看