The sum according to the condition in the matrix?

problem description

The

matrix is as follows:

1,2017,2,5
1,2018,1,5
1,2018,2,4
2,2017,1,5
1,2018,1,2

according to the first three columns, there are three kinds of matrix to produce a new matrix:

1,2017,2,5
1,2018,1,7
1,2018,2,4
2,2017,1,5

one line is missing because as long as the first three columns are the same, the fourth column summation, 2-5-7

how is it calculated?

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

Mar.28,2021

is the idea of summation in groups.

m = [[1,2017,2,5],[1,2018,1,5],[1,2018,2,4],[2,2017,1,5],[1,2018,1,2]]

-sharp dict,
-sharp key, value
d = dict()
for i in m:
    d_key = tuple(i[:-1])
    d_value = i[-1]
    if d_key not in d:
        d.update({d_key:d_value})
    else:
        d[d_key] += d_value
        
 -sharp dict
result = list()
for d_key,d_value in d.items():
    tmp = list(d_key)
    tmp.append(d_value)
    result.append(tmp)
print(result)   

result is:

[[1, 2018, 2, 4], [1, 2018, 1, 7], [2, 2017, 1, 5], [1, 2017, 2, 5]]
Menu