python - 是什么导致 Python 提示在没有列表的代码中列出列表?

标签 python numpy typeerror

在我写的文件中(从实际代码中剥离)

e = numpy.zeros(4)
n = e.copy()
n[0]=e[0]
for i in range(len(e)-1)+1:
    n[i] = n[i-1]*e[i] + e[i]

当我运行 .py 文件时,Python 说:

>>> for i in range(len(e)-1)+1:
...     n[i] = n[i-1]
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "int") to list

奇怪的是,如果我将 "i=1"(或其他值)和执行实际工作的一行粘贴到 Python 提示符中,则没有错误。无论如何,我没有看到任何列表;都是 numpy 数组。

最佳答案

range(len(e)-1) + 1

这是将一个整数 (1) 连接到一个列表 (range(...))。

也许你打算这样做?

for i in range(1, len(e)):

关于python - 是什么导致 Python 提示在没有列表的代码中列出列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14905705/

相关文章:

python - Pymongo 匹配和排序

Python:如何将大文本输出格式化为 'prettier' 和用户定义

python - 寻找方程的自洽解

javascript - 类型错误 clientsettings.userStore 不是 ng-oidc-client JS lib 的函数

javascript - 如何在 JavaScript 中插入二维数组的第二维?

python - 如何将一个列表添加到另一个列表

python - 落在网格网格之外的数据点被插值,而网格网格肯定覆盖了这些点

python - 在 Python 中读取具有特定序列的二进制文件

python - np.argsort() 的奇怪/错误输出

python - 类型错误 PYOMO : Defining constraints based on pandas dataframe