实现说明
各个操作都建立在顶层绑定之上:scores、weights、legs_kmh、growth。
使用要点
- 直接运行文件,输出会带上标签,比如
weighted =、plain fmean =、harmonic =——输出的每一行都能指回产生它的那次调用。
常见问题
有语言之外需要解析的导入吗?
没有。每个导入都解析到语言自带的模块。 文件的状态由几个模块级绑定持有:scores、weights、legs_kmh。
代码示例
from statistics import fmean, geometric_mean, harmonic_mean
# Weighted average: projects count more than maintenance work
scores = [3.2, 4.5, 4.0]
weights = [1, 3, 2]
print(f"weighted = {fmean(scores, weights=weights):.3f}")
# Unweighted average of the same scores for contrast
print(f"plain fmean = {fmean(scores):.3f}")
# Harmonic mean is the honest average speed for equal-distance legs
legs_kmh = [60, 120]
print(f"harmonic = {harmonic_mean(legs_kmh)}")
# Geometric mean fits growth factors: 2x then 8x is 4x per step
growth = [2.0, 8.0]
print(f"geometric = {geometric_mean(growth)}")