您不能从列表中减去列表。
>>> [3, 7] - [1, 2]Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for -: 'list' and 'list'
简单的方法是使用
numpy:
>>> import numpy as np>>> np.array([3, 7]) - np.array([1, 2])array([2, 5])
您也可以使用列表推导,但需要在函数中更改代码:
>>> [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]]))



