def _aggregate(data, norm=True, sort_by='<STR_LIT:value>', keys=None):
if keys:<EOL><INDENT>vdict = {k: <NUM_LIT:0> for k in keys}<EOL>for d in data:<EOL><INDENT>if d in keys:<EOL><INDENT>vdict[d] += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>vdict = {}<EOL>for d in data:<EOL><INDENT>vdict[d] = vdict[d] + <NUM_LIT:1> if d in vdict else <NUM_LIT:1><EOL><DEDENT><DEDENT>vals = [(k, v) for k, v in vdict.items()]<EOL>if sort_by == '<STR_LIT:value>':<EOL><INDENT>vals.sort(key=lambda x: x[<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>vals.sort(key=lambda x: x[<NUM_LIT:1>])<EOL><DEDENT>xs = [v[<NUM_LIT:0>] for v in vals]<EOL>if norm:<EOL><INDENT>raw_y = [v[<NUM_LIT:1>] for v in vals]<EOL>total_y = sum(raw_y)<EOL>ys = [<NUM_LIT> * y / total_y for y in raw_y]<EOL><DEDENT>else:<EOL><INDENT>ys = [v[<NUM_LIT:1>] for v in vals]<EOL><DEDENT>return xs, ys<EOL>
Counts the number of occurances of each item in 'data'.

Inputs
data: a list of values.
norm: normalize the resulting counts (as percent)
sort_by: how to sort the retured data. Options are 'value' and 'count'.

Output
a non-redundant list of values (from 'data') and a list of counts.