圣者
精华
|
战斗力 鹅
|
回帖 0
注册时间 2019-6-1
|
- #! /usr/bin/python3
- # -*- coding: utf-8 -*-
- n = 1
- input_list = []
- while True:
- inputChar = input('输入数组第' + str(n) + '个元素,直接回车结束输入\n')
- if inputChar == '':
- break
- input_list.append(inputChar)
- n += 1
- print(input_list)
- countDict = {}
- for item in input_list:
- if item in countDict:
- countDict[item] += 1
- else:
- countDict[item] = 1
- maxCount = max(countDict.values())
- for key, value in countDict.items():
- if value == maxCount:
- print("出现最多的元素是:", key, ",出现次数为", value)
- countDict.pop(key)
- break
- maxCount = max(countDict.values())
- for key, value in countDict.items():
- if value == maxCount:
- print("出现第二多的元素是:", key, ",出现次数为", value)
- countDict.pop(key)
- break
复制代码
然而最简单的方法是from collections import Counter 
|
|