Problem Description:
When directly taking the mode of all elements in the list, the result index obtained by the code written by myself is very poor, and the accuracy rate is very low. Rewritten into a library function, the accuracy rate is improved
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
Cause Analysis:
I didn’t analyze it. It may be that I wrote something wrong. I will use the official built-in function as much as possible in the future.
solution:
from collections import Counter
...
# 去掉个位数
_line =[(item // 10) *10 for item in line1]
# 使用库函数
maxKey = Counter(_line).most_common(1)[0][0]
Counter usage example: