1

This function receives a numeric matrix represented as a list of rows, where each row is in turn a list. Assume that it is a square matrix: all rows have the same length and there are as many rows as elements in each row. Also assume that the matrix is at least of dimension 2 by 2 (i.e. minimally the matrix has 2 rows with 2 elements each) The function should return a list with as many elements as number of rows. Element i in the resulting list should have the sum of the values in row i.

For example, if the matrix is

1   2   3
10  20  30
100 200 300

then this function should return the list [6,60,600]

That is, addValuesInAllRows( [ [1,2,3], [10,20,30], [100,200,300] ] ) should return [6,60,600]

Isn't this sort of similar but how would you sum up the list individually

4
  • 1
    You've changed your question so much that now all these answers are invalid.
    – Aesthete
    Nov 23, 2012 at 4:38
  • I don't understand how to add the row whole row together because I keep getting error Nov 23, 2012 at 4:41
  • @user1790201 - Read the answers people have written for you, there are solutions for both of your questions.
    – Aesthete
    Nov 23, 2012 at 4:43
  • Or I mean the individual lists together, than returning them all together Nov 23, 2012 at 4:43

3 Answers 3

4
matrix = [ [1,2,3], [10,20,30], [100,200,300] ]
print [sum(row) for row in zip(*matrix)]
0
2

Sum of columns

>>> def addValuesInAllCols(arr):
      return [sum(x) for x in zip(*arr)]

>>> addValuesInAllCols( [ [1,2,3], [10,20,30], [100,200,300] ] )
[111, 222, 333]

Sum of rows

>>> map(sum, [ [1,2,3], [10,20,30], [100,200,300] ] )
[6, 60, 600]
0

One more option:

from operator import itemgetter

matrix = [ [1,2,3], [10,20,30], [100,200,300] ]

def addValuesInAllCols(arr):
    return map(sum, [map(itemgetter(i), arr) for i in range(len(a))])

Where map is a build-in function that can be rewritten as a simple for. For ex:

[map(itemgetter(i), arr) for i in range(len(a))] is the same as:

result = []
for i in range(len(a)):
    tmp = []
    for row in a:
        tmp.append(row[i])
    result.append(tmp)

Edited per new tests:

def addValuesInAllCols(arr):
    return map(sum, arr)

Or without map:

def addValuesInAllCols(arr):
    return [sum(row) for row in arr]
4
  • I can't use imported functions, but I can see where this is going but what does map do? Nov 23, 2012 at 4:35
  • I used your alternate function without map, and for the example I showed above I got [[1, 10, 100], [2, 20, 200], [3, 30, 300]] instead of [6, 60 , 600] Nov 23, 2012 at 4:55
  • @user1790201 that sample is for your very first sample that you changed than. The same for new tests is 'return [sum(row) for row in arr]' Nov 23, 2012 at 4:57
  • 1
    @user1790201 you are welcome, if my post was helpful - vote it))) Nov 23, 2012 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.