def pivot(table, left, top, value):
    rs = {}
    ysort = []
    xsort = []
    for row in table:
        yaxis = tuple([row[c] for c in left])
        if yaxis not in ysort: ysort.append(yaxis)
        xaxis = tuple([row[c] for c in top])
        if xaxis not in xsort: xsort.append(xaxis)
        try:
            rs[yaxis]
        except KeyError:
            rs[yaxis] = {}
        if xaxis not in rs[yaxis]:
            rs[yaxis][xaxis] = 0
        rs[yaxis][xaxis] += row[value]
    for key in rs:
        if len(rs[key]) - len(xsort):
            for var in xsort:
                if var not in rs[key].keys():
                    rs[key][var] = ''
    headings = list(left)
    headings.extend(xsort)
    t = []
    for left in ysort:
        row = list(left)
        sortedkeys = sorted(rs[left].keys())
        sortedvalues = map(rs[left].get, sortedkeys)
        row.extend(sortedvalues)
        t.append(dict(zip(headings,row)))
    return t