class Solution(object):
def average(self, salary):
salary.sort()
salary = salary[1:-1]
from decimal import *
result = Decimal(sum(salary))/Decimal(len(salary))
return result
思路很直白。二刷的时候可以考虑不做排序。
备注:这里如果不用decimal的话会导致小数位数不精确。原理:计算机保存数据是用2进制,而显示是用10进制,会导致误差。
详细原理可见:
1 浮点数误差原理:
Python 为什么不解决四舍五入(round)的“bug”_ReedSun的博客-CSDN博客
2 解决方案:
Python decimal模块使用方法详解 - 知乎 (zhihu.com)
3 import xx as xx / from xx import * 区别:
import xx as xx / from xx import * 区别_晨风默语的博客-CSDN博客



