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]
sums_in_rows = list(map(sum, table1d))- furasTypeError: unsupported operand type(s) for +: 'int' and 'list'becausesumRowsis an int andtable1d[i]is a list. That's beside the point tho because @furas is rightsums_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. - kpiearray.sum(axis=0),array.sum(axis=1)and total sumarray.sum()- furas