def _nonzero_avg(items, div=None):
    """Returns the average of a list of items, excluding
    zeros. The optional div argument can be set to a list of
    values to divide each item by when computing the
    average."""
    assert div is None or len(items) == len(div)
    s = 0.0
    c = 0
    for i, val in enumerate(items):
        assert val >= 0
        if val != 0:
            div_i = 1 if div is None else div[i]
            s += val / float(div_i)
            c += 1
    if c == 0:
        return 0
    return s / float(c)
