Neil Webster a écrit :
Thanks for the help so far.

The background to the problem is that the lists come from reading a
dbf file.  The code that I am trying to write is to merge lines of the
dbf based on the first column.  So in my example there would be three
lines:
a 2 3 4
b 10 11 12
a 2 3 4

The expected output from the above example lines would be:
a 4 6 8
b 10 11 12

 ... and the lines are read as: [[a,2,3,4],[b,10,11,12], [a,2,3,4]]


If you don't care about the original ordering, the following code should do.

# 8<----------------------------------------------------------------

def merge_rows(rows):
    merged = dict()
    for row in rows:
        key, values = row[0], row[1:]
        sums = merged.setdefault(key, [0, 0, 0])
        for i, v in enumerate(values):
            sums[i] += v

    return [key] + value for key, value in merged.iteritems()]

import sys
def print_rows(rows, out=sys.stdout):
    for row in rows:
        print " ".join(map(str, row))

if __name__ == '__main__':
    inputs = [['a',2,3,4],['b',10,11,12], ['a',2,3,4]]
    expected = [['a',4,6,8],['b',10,11,12]]
    print "inputs : "
    print_rows(inputs)

    outputs = merge_rows(inputs)
    outputs.sort() # so we can do a simple equality test
    assert outputs == expected, "Expected %s, got %s" % (
       expected, outputs
       )

    print "outputs :"
    print_rows(outputs)

# 8<----------------------------------------------------------------


In response to not posting working code or actual inputs, ummm, that's
why I am asking the question here.

In this context, "working code" means "minimal code that a) can be executed and b) exhibits the problem you have, whatever the problem is". This code should include or be shipped with example input data and corresponding expected output data - as I did in the above code.


HTH
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to