TypeError: unsupported operand type(s) for -: ‘int‘ and ‘list‘ 解决过程

让我们以下面的例子进行说明:

a=[1,2,3]

b=np.array([4,5,6])

运行:
c=1-a

结果报错:
TypeError: unsupported operand type(s) for -: ‘int’ and ‘list’

在这里插入图片描述

运行:
c=1-b

结果如下:
array([-3, -4, -5])

错误表示:不支持 int整型和 list列表的 减法运算,列表不是numpy的数组,没有广播运算。
因此需要将列表列表变成numpy数组,进行广播运算,就不会报错了。

在这里插入图片描述
同理,对于加法也是一样的:

TypeError: unsupported operand type(s) for +: ‘int’ and ‘list’