Home > Back-end >  Stuck with non subscriptable
Stuck with non subscriptable

Time:11-22

def div_mat_by_scalar(mat, alpha):
    mat2=[]
    for row in range (len(mat)):
        for collum in range (len (mat[0])):
            mat2[collum[row]]=(mat[collum[row]]/alpha)
    return mat2
mat1 = [[2, 4], [6, 8]]
mat2 = div_mat_by_scalar(mat1, 2)
print(mat1 == [[2, 4], [6, 8]])
print(mat2 == [[1, 2], [3, 4]])

I am trying to divide a matrix (a list of lists) by a number, without using numpy, however I keep getting this error:

TypeError: 'int' object is not subscriptable

I need help.

I tried two loops to go over all the columns and rows, and just return a new matrix with the result.

CodePudding user response:

you are trying to access an element of a number when doing the following

mat2[collum[row]] --> Change To --> mat2[collum][row]

So

mat2[collum[row]]=(mat[collum[row]]/alpha)

becomes

mat2[collum][row]=(mat[collum][row]/alpha)

CodePudding user response:

To get a number from matrix you should have mat2[collum][row] instead of mat2[collum[row]]

CodePudding user response:

You can access an element of your list in this way:

mat2[collumn]

It returns the collumnth list of mat2 list. Then to access the rowth element of this list:

mat2[collumn][row]

CodePudding user response:

You are getting the error TypeError: 'int' object is not subscriptable because you are trying to subscript an int with another int here collum[row], here both collum and row are of type int. You have to access two-dimensional lists in python as mat[row][collum]. Your code should look like.

def div_mat_by_scalar(mat, alpha):
    mat2=[]
    for row in range (len(mat)):
        mat2.append([])
        for collum in range (len(mat[row])):
            mat2[row].append(mat[row][collum]/alpha)
    return mat2
mat1 = [[2, 4], [6, 8]]
mat2 = div_mat_by_scalar(mat1, 2)
print(mat1 == [[2, 4], [6, 8]])
print(mat2 == [[1, 2], [3, 4]])

CodePudding user response:

instead of the whole function you created you can also search for what's already created :

you can do it with numpy.divide, it gives you the expected output:

import numpy as np

mat2=np.divide(mat1,2)

CodePudding user response:

Even if you correct the indexes as in other answers, you cannot add values to empty list using your method.

After doing this-->

mat2=[]

This assignment doesn't work-->

mat2[collum][row]=(mat[collum][row])/alpha

But what you can do is:--> append operation

mat2.append(12) #for 1D
mat2[1].append(77) #for 2D

If you seek easy way instead of looping use numpy -->

mat2 = np.asarray(mat1)/alpha

Also matrix data type doesn't exist, list is used instead

CodePudding user response:

mat is 2d array you are accessing it wrong here: mat[collum[row]]

instead try this:

def div_mat_by_scalar(mat, alpha):

mat2=[]

for row in range (len(mat)):

    mat2.append([])

    for collum in range (len (mat[0])):

        mat2[row].append(mat[row][collum]//alpha)

return mat2

mat1 = [[2, 4], [6, 8]]

mat2 =div_mat_by_scalar(mat1, 2)

print(mat1 == [[2, 4], [6, 8]])

print(mat2 == [[1, 2], [3, 4]])

  • Related