<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def losses_by_tag(dstore, tag):
 """ Statistical average losses by tag. For instance call $ oq extract losses_by_tag/occupancy """

    dt = [(tag, vstr)] + dstore['oqparam'].loss_dt_list()
    aids = dstore['assetcol/array'][tag]
    dset, stats = _get(dstore, 'avg_losses')
    arr = dset.value
    tagvalues = dstore['assetcol/tagcol/' + tag][1:]  # except tagvalue="?"
    for s, stat in enumerate(stats):
        out = numpy.zeros(len(tagvalues), dt)
        for li, (lt, lt_dt) in enumerate(dt[1:]):
            for i, tagvalue in enumerate(tagvalues):
                out[i][tag] = tagvalue
                counts = arr[aids == i + 1, s, li].sum()
                if counts:
                    out[i][lt] = counts
        yield stat, out