(data, weights=None)
return _convert(sum_weights / total, T)
Return the harmonic mean of data.

    The harmonic mean is the reciprocal of the arithmetic mean of the
    reciprocals of the data.  It can be used for averaging ratios or
    rates, for example speeds.

    Suppose a car travels 40 km/hr for 5 km and then speeds-up to
    60 km/hr for another 5 km. What is the average speed?

        >>> harmonic_mean([40, 60])
        48.0

    Suppose a car travels 40 km/hr for 5 km, and when traffic clears,
    speeds-up to 60 km/hr for the remaining 30 km of the journey. What
    is the average speed?

        >>> harmonic_mean([40, 60], weights=[5, 30])
        56.0

    If ``data`` is empty, or any element is less than zero,
    ``harmonic_mean`` will raise ``StatisticsError``.
Return the harmonic mean of data.
Return the harmonic mean of data .
def harmonic_mean(data, weights=None):
    """Return the harmonic mean of data.

    The harmonic mean is the reciprocal of the arithmetic mean of the
    reciprocals of the data.  It can be used for averaging ratios or
    rates, for example speeds.

    Suppose a car travels 40 km/hr for 5 km and then speeds-up to
    60 km/hr for another 5 km. What is the average speed?

        >>> harmonic_mean([40, 60])
        48.0

    Suppose a car travels 40 km/hr for 5 km, and when traffic clears,
    speeds-up to 60 km/hr for the remaining 30 km of the journey. What
    is the average speed?

        >>> harmonic_mean([40, 60], weights=[5, 30])
        56.0

    If ``data`` is empty, or any element is less than zero,
    ``harmonic_mean`` will raise ``StatisticsError``.
    """
    if iter(data) is data:
        data = list(data)
    errmsg = 'harmonic mean does not support negative values'
    n = len(data)
    if n < 1:
        raise StatisticsError('harmonic_mean requires at least one data point')
    elif n == 1 and weights is None:
        x = data[0]
        if isinstance(x, (numbers.Real, Decimal)):
            if x < 0:
                raise StatisticsError(errmsg)
            return x
        else:
            raise TypeError('unsupported type')
    if weights is None:
        weights = repeat(1, n)
        sum_weights = n
    else:
        if iter(weights) is weights:
            weights = list(weights)
        if len(weights) != n:
            raise StatisticsError('Number of weights does not match data size')
        _, sum_weights, _ = _sum(w for w in _fail_neg(weights, errmsg))
    try:
        data = _fail_neg(data, errmsg)
        T, total, count = _sum(w / x if w else 0 for w, x in zip(weights, data))
    except ZeroDivisionError:
        return 0
    if total <= 0:
        raise StatisticsError('Weighted sum must be positive')
    return _convert(sum_weights / total, T)
def harmonic_mean ( data , weights = None ) : if iter ( data ) is data : data = list ( data ) errmsg = 'harmonic mean does not support negative values' n = len ( data ) if n < 1 : raise StatisticsError ( 'harmonic_mean requires at least one data point' ) elif n == 1 and weights is None : x = data [ 0 ] if isinstance ( x , ( numbers . Real , Decimal ) ) : if x < 0 : raise StatisticsError ( errmsg ) return x else : raise TypeError ( 'unsupported type' ) if weights is None : weights = repeat ( 1 , n ) sum_weights = n else : if iter ( weights ) is weights : weights = list ( weights ) if len ( weights ) != n : raise StatisticsError ( 'Number of weights does not match data size' ) _ , sum_weights , _ = _sum ( w for w in _fail_neg ( weights , errmsg ) ) try : data = _fail_neg ( data , errmsg ) T , total , count = _sum ( w / x if w else 0 for w , x in zip ( weights , data ) ) except ZeroDivisionError : return 0 if total <= 0 : raise StatisticsError ( 'Weighted sum must be positive' ) return _convert ( sum_weights / total , T )