python unsupported operand type(s) for -: 'list' and 'list'
时间: 2024-01-24 18:03:02 浏览: 147
python-type-checking:Python类型检查指南的源文件-python source file
这个报错通常会在两个列表相减时出现,因为列表之间不能直接做减法。你可以通过使用列表推导式或循环来逐个计算列表元素之间的差值。例如:
a = [1, 2, 3]
b = [3, 2, 1]
c = [a[i] - b[i] for i in range(len(a))] # c = [-2, 0, 2]
或者:
a = [1, 2, 3]
b = [3, 2, 1]
c = []
for i in range(len(a)):
c.append(a[i] - b[i])
# c = [-2, 0, 2]
阅读全文