0
votes

I would like to print sum of each row and sum of each column of a two dimensional array, like this:

  • sum row1 = 123 (numbers are not real sums, just for example)
  • sum row2 = 123
  • sum row3 = 123

And the same with columns. I know how to do it in java, but dont know how to do it in python. This is my code(missing code for sums of rows and columns, because I dont know how to do it):

from random import randint

dim1 = input("Insert first dimension: ")
dim1 = int(dim1)

dim2 = input("Insert second dimension: ")
dim2 = int(dim2)

table1d = []

for i in range(dim1):
    table2d = []
    for j in range(dim2):
        table2d.append(randint(1, 170))
    table1d.append(table2d)
print(table1d)

totalSum = sum(map(sum, table1d))
print(totalSum)

sumRows = 0
for i in range(0, len(table1d), 1):
    sumRows += table1d[i]
4
sums_in_rows = list(map(sum, table1d)) - furas
Looks like the last line is TypeError: unsupported operand type(s) for +: 'int' and 'list' because sumRows is an int and table1d[i] is a list. That's beside the point tho because @furas is right sums_in_rows = map(sum, table1d) is how you sum the rows and you can sum the columns the same way after simply transposing the data. - kpie
convert it to numpy array and you will have array.sum(axis=0), array.sum(axis=1) and total sum array.sum() - furas
Yes I know it is an error, because I dont know how to do it :) Is it the same for rows and columns, so for columns I would do it like this: sums_in_columns = lmap(sum, table2d)? How do I convert it to numpy array? - Escape

4 Answers

0
votes

For rows you need only

sums_in_rows = list(map(sum, table1d))

print(sums_in_rows)

For columns it needs more

sums_in_columns = [0]*len(table1d[0])  # create list for all results

for row in table1d:
    for c, value in enumerate(row):
        sums_in_columns[c] += value

print(sums_in_columns)

You can also convert it to numpy array and then you have

import numpy as np

arr = np.array(table1d)

print('rows:',  arr.sum(axis=1))
print('cols:',  arr.sum(axis=0))
print('total:', arr.sum())

from random import randint

dim1 = input("Insert first dimension: ")
dim1 = int(dim1)

dim2 = input("Insert second dimension: ")
dim2 = int(dim2)

table1d = []
#x = 0
for i in range(dim1):
    table2d = []
    for j in range(dim2):
        table2d.append(randint(1, 170))
        #table2d.append(x)
        #x += 1
    table1d.append(table2d)
print(table1d)

sums_in_rows = list(map(sum, table1d))
print(sums_in_rows)

sums_in_columns = [0]*len(table1d[0])
for row in table1d:
    for c, value in enumerate(row):
        sums_in_columns[c] += value
print(sums_in_columns)

import numpy as np

arr = np.array(table1d)
print(arr.sum(axis=1))
print(arr.sum(axis=0))
print(arr.sum())
0
votes
import numpy as np

Columns:

np.array(table1d).sum(axis=0)

Rows:

np.array(table1d).sum(axis=1)
0
votes

You can use list comprehensions and the sum function to obtain the desired result:

import random

rowCount = 3
colCount = 5

matrix = [ [random.randint(10,99) for _ in range(colCount)] for _ in range(rowCount) ]

for line in matrix:
    print(line)

for row in range(rowCount):
    print(f"sum row{row} = ",sum(matrix[row]))

for col in range(colCount):
    print(f"sum column{col} = ",sum(row[col] for row in matrix))


[90, 62, 86, 19, 13]
[33, 93, 38, 17, 29]
[11, 96, 91, 66, 81]

sum row0 =  270
sum row1 =  210
sum row2 =  345

sum column0 =  134
sum column1 =  251
sum column2 =  215
sum column3 =  102
sum column4 =  123
0
votes

Here is a simple and straightforward two-loop solution, if you want to do both the sums together.

container = [[1, 2, 3, 4],
             [3, 2, 1, 5],
             [4, 5, 6, 6]]
rowSum, colSum, i = [0]*len(container), [0]*len(container[0]), 0
while i < len(container):
    j = 0
    while j < len(container[0]):
        rowSum[i] += container[i][j]
        colSum[j] += container[i][j]
        j += 1
    i += 1
print(rowSum, colSum)

Hope it helps. ✌