I am trying to implement the Naive Gauss and getting the unsupported operand type error on execution. Output:
execfile(filename, namespace) File "/media/zax/MYLINUXLIVE/A0N-.py", line 26, in <module> print Naive_Gauss([[2,3],[4,5]],[[6],[7]]) File "/media/zax/MYLINUXLIVE/A0N-.py", line 20, in Naive_Gauss b[row] = b[row]-xmult*b[column] TypeError: unsupported operand type(s) for -: 'list' and 'list' >>>
This is the code
def Naive_Gauss(Array,b): n = len(Array) for column in xrange(n-1): for row in xrange(column+1, n): xmult = Array[row][column] / Array[column][column] Array[row][column] = xmult #print Array[row][col] for col in xrange(0, n): Array[row][col] = Array[row][col] - xmult*Array[column][col] b[row] = b[row]-xmult*b[column] print Array print b print Naive_Gauss([[2,3],[4,5]],[[6],[7]])
The Python "TypeError: unsupported operand type(s) for /: 'str' and 'int'" occurs when we try to use the division / operator with a string and a number. To solve the error, convert the string to an int or a float , e.g. int(my_str) / my_num . Here is an example of how the error occurs.
The Python "TypeError: unsupported operand type(s) for /: 'list' and 'int'" occurs when we try to use the division / operator with a list and a number. To solve the error, figure out how the variable got assigned a list and correct the assignment, or access a specific value in the list.
The Python "TypeError: unsupported operand type(s) for -: 'str' and 'str'" occurs when we try to use the subtraction - operator with two strings. To solve the error, convert the strings to int or float values, e.g. int(my_str_1) - int(my_str_2) .
Unsupported operand type(s) for +: 'NoneType' and 'int' # The Python "TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'" occurs when we try to use the addition (+) operator with a None value. To solve the error, figure out where the variable got assigned a None value and correct the assignment.
You can't subtract a list from a list.
>>> [3, 7] - [1, 2] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for -: 'list' and 'list'
Simple way to do it is using numpy
:
>>> import numpy as np >>> np.array([3, 7]) - np.array([1, 2]) array([2, 5])
You can also use list comprehension, but it will require changing code in the function:
>>> [a - b for a, b in zip([3, 7], [1, 2])] [2, 5]
>>> import numpy as np >>> >>> def Naive_Gauss(Array,b): ... n = len(Array) ... for column in xrange(n-1): ... for row in xrange(column+1, n): ... xmult = Array[row][column] / Array[column][column] ... Array[row][column] = xmult ... #print Array[row][col] ... for col in xrange(0, n): ... Array[row][col] = Array[row][col] - xmult*Array[column][col] ... b[row] = b[row]-xmult*b[column] ... print Array ... print b ... return Array, b # <--- Without this, the function will return `None`. ... >>> print Naive_Gauss(np.array([[2,3],[4,5]]), ... np.array([[6],[7]])) [[ 2 3] [-2 -1]] [[ 6] [-5]] (array([[ 2, 3], [-2, -1]]), array([[ 6], [-5]]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With