I have this list of average water flow values in gallons from a set of 5 garden hoses:
[6.6, 14, 12.6, 8.8, 11.8]
I want to calculate the subcontrary/harmonic mean of this list and I found this answer that works:
def hmean(args):
return len(args) / sum(1. / val for val in args)
hose_flows = [6.6, 14, 12.6, 8.8, 11.8]
print(hmean(hose_flows)) # 9.986200495805194
However I don't find this very elegant... There surely is a more elegant way to do this?
If you're using Python 3.6.0 or above you can use statistics.harmonic_mean:
>>> import statistics
>>> hose_flows = [6.6, 14, 12.6, 8.8, 11.8]
>>> statistics.harmonic_mean(hose_flows)
9.986200495805194
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With