【问题标题】:TypeError: unsupported operand type(s) for -: 'list' and 'list'TypeError: 不支持的操作数类型 -: 'list' 和 'list'
【发布时间】:2014-12-28 10:09:31
【问题描述】:

我正在尝试实现朴素高斯并在执行时遇到不支持的操作数类型错误。 输出:

  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'
>>>   

这是代码

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]])

【问题讨论】:

  • 这是您的问题行:b[row] = b[row]-xmult*b[column] row 是一个列表,而 b[column] 是一个列表,因此您试图从另一个列表中减去一个列表(如错误输出所示you) 不是受支持的操作。
  • 谢谢@JonKiparsky,这真的很有帮助

标签: python python-2.7 list typeerror operands


【解决方案1】:

需要执行的操作,需要通过

创建的 numpy 数组

np.array()

或者可以通过

从列表转换为数组

np.stack()

与上述情况一样,输入 2 个列表作为操作数会触发错误。

【讨论】:

    【解决方案2】:

    这个问题已经回答了,但我觉得我还应该提到另一个潜在的原因。这是遇到相同错误消息但出于不同原因的直接结果。如果您的列表为空,则不会执行该操作。检查您的代码是否有缩进和拼写错误

    【讨论】:

      【解决方案3】:

      在 Python 中使用 Set

      >>> a = [2,4]
      >>> b = [1,4,3]
      >>> set(a) - set(b)
      set([2])
      

      【讨论】:

        【解决方案4】:

        你不能从一个列表中减去一个列表。

        >>> [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]]))
        

        【讨论】:

          猜你喜欢
          • 2012-12-12
          • 2016-05-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-31
          相关资源
          最近更新 更多