-6

I would like to write a piece of code which calculates the sum of the elements in each row of a list and returns a new list of the row sums. For example

def row_sums(square):

    square = [
       [1, 2, 3, 4],
       [5, 6, 7, 8],
       [9, 10, 11, 12],
       [13, 14, 15, 16]
    ]
    print(row_sums(square))

This would give the output of [10, 26, 42, 58] As the sum of the first row equals 10, sum of the second row equals 26 and so on. However I would like to NOT use the built in sum function to do this. How would I go about doing this? Thanks in advance.

cs95
  • 379,657
  • 97
  • 704
  • 746
Hoist
  • 29
  • 1
  • 2
  • 6

6 Answers6

6

A simple piece of code for calculating the sum of the elements in each row of a list.

square = [
   [1, 2, 3, 4],
   [5, 6, 7, 8],
   [9, 10, 11, 12],
   [13, 14, 15, 16]
]
su=[sum(i) for i in square]
print (su)

Output:

[10, 26, 42, 58]
Rohit-Pandey
  • 2,039
  • 17
  • 24
3

If you really cant use the sum() function, here is a function to sum the rows, I've written it explicitly to show the steps but it would be worth looking at list comprehensions once you understand what is going on:

def row_sums(square):

    # list to store sums
    output = []

    # go through each row in square
    for row in square:

        # variable to store row total
        total = 0

        # go through each item in row and add to total
        for item in row:
            total += item

        # append the row's total to the output list
        output.append(total)

    # return the output list
    return output

This can then be used as such:

square = [
   [1, 2, 3, 4],
   [5, 6, 7, 8],
   [9, 10, 11, 12],
   [13, 14, 15, 16]
]

row_totals = row_sums(square)

EDIT:

In answer to your comment, I'd do something like this:

def sum_columns(square):

    # as before have a list to store the totals
    output = []

    # assuming your square will have the same row length for each row
    # get the number of columns
    num_of_columns = len(square[0])

    # iterate over the columns
    for i in xrange(0, num_of_columns):

        # store the total for the column (same as before)
        total = 0

        # for each row, get the value for the column and add to the column total
        # (value at index i)
        for row in square:
            total += row[i]

        # append the total to the output list
        output.append(total)

    # return the list of totals
    return output        
RHSmith159
  • 1,823
  • 9
  • 16
  • Thanks for your reply, however I have another question. How would I adjust this code to add the elements in each column rather than row? – Hoist Aug 23 '17 at 10:37
  • edited my question with an answer, as I mentioned before, I've written it out step by step so you can see the logic, once you're happy with how it works, it'd be worth looking at some of these other answers that do it more concisely :) – RHSmith159 Aug 23 '17 at 10:55
2

Write your own sum function...

The module functools has a useful reduce function that you can use to write your own sum function. If you are comfortable with lambda functions you could do it this way:

lst = [0,1,2,3,4,5]

which would give sum(lst) as 15. However your own sum function with reduce may look something like:

from functools import reduce

reduce(lambda x,y: x + y, l)

which woudld also give 15. You should be able to write the rest yourself (i.e it within another list working on rows).

Community
  • 1
  • 1
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
1

Well at a certain point you have to use the sum-function (either as sum() or "+") but you could use map like

list(map(sum, square))
Rohit-Pandey
  • 2,039
  • 17
  • 24
david
  • 113
  • 10
1

You can also do it with comprehension and reduce:

[reduce(lambda x, y: x + y, item) for item in square]
zipa
  • 27,316
  • 6
  • 40
  • 58
1

You can add that lines to your existent function:

result = []
for row in square: # iterates trough a row
    line = 0 #stores the total of a line

    for num in row: #go trough every number in row
        line += num #add that number to the total of that line

    result.append(line) #append to a list the result

print(result) #finally return the total of every line sum's in a list