-1
import random
n=int(input("Enter the limit of your matrix:"))
a=[[random.random()for i in range(n)],[random.random()for j in range(n)]]
for i in range(0,n):
    for j in range(0,n):
        a[i][j]=int(input("Enter the element:"))
sumr=0
sumc=0
for i in range(0,n):
    sumr+=a[i]
for j in range(0,n):
    for i in range(0,n):
        sumc+=a[j][i]

this program shows attribute error that list cannot be added to int item. help me in rectifying it. also suggest the corrections to be made

Surkaav S
  • 49
  • 1
  • 8

6 Answers6

4

a[i] is a list (representing a row in your code).

You cannot add list and int, you should:

sumr += sum(a[i])

sum will return the sum of elements in the list a[i] as an int, and now your calculations will be fine.

Maroun
  • 94,125
  • 30
  • 188
  • 241
2

Instead of

for i in range(0,n):
    sumr+=a[i]

Simply write

sumr = sum(a[i] for i in range(0, n))

a[i] is a list. You can't add a list to an int directly, even if it's a list of int. Conversely, you can add the sum() of a list of int to an int.

Jivan
  • 21,522
  • 15
  • 80
  • 131
  • 1
    `sum([ a[i] for i in range (0,n) ])` is redundant. `sum(a[i])` does the same thing. – Alex Dec 30 '14 at 12:18
2
$ python test.py 
Enter the limit of your matrix:3
Enter the element:1
Enter the element:2
Enter the element:3
Enter the element:1
Enter the element:2
Enter the element:3
Enter the element:1
Traceback (most recent call last):
  File "test.py", line 209, in <module>
    a[i][j]=int(input("Enter the element:"))
IndexError: list index out of range

Your code work for 2*2 matrix, because you crested only two rows.

a=[[random.random()for i in range(n)],[random.random()for j in range(n)]]

Following is output of a for 3*3 matrix:-

[[0.9632434262652646, 0.7470504655963679, 0.2750109619917276], [0.7032133906246875, 0.16200573351318048, 0.09565026547688305]]

overcome to above issue see following code: Algo/steps:

  1. Get Matrix limit n*n
  2. Create matrix of n*n
  3. By lambda, map and sum function get sum of all rows and column.
  4. No need of random method.

code:

n = int(raw_input("Enter the limit of your matrix:"))
a = []
tmp = [0 for k in range(n)]
for i in range(0,n):
    a.append(list(tmp))
    for j in range(0,n):
        a[i][j] = int(input("Enter the element:"))

sumr = sum(map(lambda x:sum(x), a))
sumc = 0
for j in range(0,n):
    sumc +=sum(map(lambda x:x[j], a))

print sumr
print sumc

Output:

Enter the limit of your matrix:3
Enter the element:1
Enter the element:2
Enter the element:3
Enter the element:1
Enter the element:2
Enter the element:3
Enter the element:1
Enter the element:2
Enter the element:3
18
18
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
0
  1. Get Matrix limit n*n
  2. Create matrix of n*n
  3. By lambda, map and sum function get sum of all rows and column.
  4. No need of random method.

>

n = int(raw_input("Enter the limit of your matrix:"))
a = []
tmp = [0 for k in range(n)]
for i in range(0,n):
    a.append(list(tmp))
    for j in range(0,n):
        a[i][j] = int(input("Enter the element:"))

sumr = sum(map(lambda x:sum(x), a))
sumc = 0
for j in range(0,n):
    sumc +=sum(map(lambda x:x[j], a))

print sumr
print sumc

Output:-

Enter the limit of your matrix:3
Enter the element:1
Enter the element:2
Enter the element:3
Enter the element:1
Enter the element:2
Enter the element:3
Enter the element:1
Enter the element:2
Enter the element:3
18
18
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
0

I have made some assumptions regarding your question, and here is a solution that prints sums of rows and columns after entering the elements. Also, you've been trying to add a list to integer, which was causing that error.

Note that we don't need random() to initialize the list, so I've replaced it. I've refactored your code a little bit and wrote comments that would help.

n = input("Enter the limit of your matrix:") # no need to cast input to int
a = [[0 for x in xrange(n)] for x in xrange(n)]
for i in xrange(n):    # range(n) or xrange(n) start from 0 by default
    for j in xrange(n):
        a[i][j]=int(input("Enter the element:"))
sumr = []
sumc = [0 for x in xrange(n)]

for i in xrange(n):
    sumr.append(sum(a[i]))
    for j in xrange(n):
        sumc[j] += a[i][j]

print "sum of rows: ", sumr
print "sum of columns", sumc

This works for any index. Also, the sumr and sumc in your code, if they're what I guess they are, would both get same value at the end, but this is different here.

Here's an example:

~ python test.py
Enter the limit of your matrix:3
Enter the element:1
Enter the element:2
Enter the element:3
Enter the element:1
Enter the element:2
Enter the element:4
Enter the element:1
Enter the element:5
Enter the element:6
sum of rows:  [6, 7, 12]
sum of columns [3, 9, 13]

Also, refer to this link for more info about how to initialize multi-dimensional lists, and some info about difference between xrange and range, although it wont make much of a difference in this example.

Community
  • 1
  • 1
Bharadwaj Srigiriraju
  • 2,196
  • 4
  • 25
  • 45
0

Your code in line 3 and line 6 are conflicting (intuitively).

a=[[random.random()for i in range(n)],[random.random()for j in range(n)]]

does not create n lists with n elements in each list(am guessing here, looks like that's what you need) This one does that:

a=[[random.random() for i in range(n)] for j in range(n)]]

The part that leads to error is:

sumr=0
sumc=0
for i in range(0,n):
    sumr+=a[i]

sumr is of <type 'int'>, but a[i] is a list of len = n, and the += operator raises the error there.

If you want sum of rows, you can try:

sumr = [sum(row) for row in a]        # sum of rows
sumc = [sum(col) for col in zip(*a)]  # sum of columns

For simpler code you must use numpy.array and numpy.sum

a = numpy.array(a)
sumr = numpy.sum(a, 1)
sumc = numpy.sum(a, 0)
Prashanth
  • 1,252
  • 2
  • 13
  • 28