Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating Subcontrary Mean from a list in Python [duplicate]

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?

like image 929
MrFromOuterSpace Avatar asked Mar 17 '26 21:03

MrFromOuterSpace


1 Answers

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
like image 75
Sash Sinha Avatar answered Mar 19 '26 10:03

Sash Sinha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!