Python 类型错误:不支持 - 的操作数类型:“列表”和“列表”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26685679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: unsupported operand type(s) for -: 'list' and 'list'
提问by Iliass
I am trying to implement the Naive Gauss and getting the unsupported operand type error on execution. Output:
我正在尝试实现 Naive Gauss 并在执行时出现不受支持的操作数类型错误。输出:
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]])
采纳答案by falsetru
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:
简单的方法是使用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]]))
回答by Harsh Kothari
Use Set in Python
在 Python 中使用 Set
>>> a = [2,4]
>>> b = [1,4,3]
>>> set(a) - set(b)
set([2])
回答by ratchet600
This question has been answered but I feel I should also mention another potential cause. This is a direct result of coming across the same error message but for different reasons. If your list/s are empty the operation will not be performed. check your code for indents and typos
这个问题已经得到回答,但我觉得我还应该提到另一个潜在的原因。这是遇到相同错误消息但出于不同原因的直接结果。如果您的列表为空,则不会执行操作。检查您的代码是否有缩进和拼写错误
回答by Khan
The operations needed to be performed, require numpy arrays either created via
需要执行的操作,需要通过创建的 numpy 数组
np.array()
np.array()
or can be converted from list to an array via
或者可以通过以下方式从列表转换为数组
np.stack()
np.stack()
As in the above mentioned case, 2 lists are inputted as operands it triggers the error.
与上述情况一样,输入 2 个列表作为触发错误的操作数。

