问题描述:
直接取列表中所有元素的众数时,自己写的代码得到的结果指标很差,准确率很低,改写成库函数,准确率得到提升
from collections import Counter
...
# 去掉个位数
_line =[(item // 10) *10 for item in line]
numcount = dict(Counter(_line))
numcount存储了出现的编号以及对应频率
maxValue = 0
maxKey = 0
# 得到出现最多的数
for key ,value in numcount.items():
if value > maxValue :
maxValue = value
maxKey = key
原因分析:
没分析出来・ࡇ・可能是自己写错了哪里,以后尽可能使用官方自带函数
解决方案:
from collections import Counter
...
# 去掉个位数
_line =[(item // 10) *10 for item in line1]
# 使用库函数
maxKey = Counter(_line).most_common(1)[0][0]
Counter用法样例: