Stuck with non subscriptable

Question:

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.

Asked By: shimmy goldsmith

||

Answers:

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

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

So

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

becomes

mat2[column][row]=(mat[column][row]/alpha)
Answered By: White Wizard

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

Answered By: EnolaT

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

mat2[column]

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

mat2[column][row]
Answered By: Amir reza Riahi

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

def div_mat_by_scalar(mat, alpha):
    mat2=[]
    for row in range (len(mat)):
        mat2.append([])
        for column in range (len(mat[row])):
            mat2[row].append(mat[row][column]/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]])

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)
Answered By: Ran A

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[column][row]=(mat[column][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

Answered By: SHIVARAJ S G

mat is 2d array you are accessing it wrong here:

mat[column[row]]

instead try this:

def div_mat_by_scalar(mat, alpha):

    mat2=[]
    
    for row in range (len(mat)):

        mat2.append([])

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

            mat2[row].append(mat[row][column]//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]])
Answered By: Aishwarya
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.