python - 它在哪里被视为一个整数?

标签 python python-3.x numpy

我看过类似问题的答案,但在我的代码中找不到将 x 视为 int 的地方。

import matplotlib.pyplot as plt
import numpy as np

def aitken(x,y,value,n):
    if n == 1:
        return y[0]
    first_array = x.pop(n-1)
    second_array = x.pop(n-2)
    first_y = y.pop(n-1)
    second_y = y.pop(n-2)
    n1 = n-1
    return (aitken(first_array,first_y,value,n1)*(value - x[n-1]) - aitken(second_array, second_y,value,n1)*(value - x[n-2]))/(x[n-1] - x[n-2])

x = [1, 4, 7, 11, 15, 20, 30, 50, 77, 92, 100]
y = [5, 20, 52, 121, 228, 403, 903, 2504, 5929, 8464, 10005]
n = len(x)
x_fit = np.arange(0,104,1)
y_fit = aitken(x,y,x_fit,n)

错误信息如下:

  File "exl.py", line 17, in <module>
    y_fit = aitken(x,y,x_fit,n)
  File "exl.py", line 13, in aitken
    return (aitken(first_array,first_y,value,n1)*(value - x[n-1]) - aitken(second_array, second_y,value,n1)*(value - x[n
-2]))/(x[n-1] - x[n-2])
  File "exl.py", line 8, in aitken
    first_array = x.pop(n-1)
AttributeError: 'int' object has no attribute 'pop'

抱歉,如果这个问题太基础了。我是 python 新手。

最佳答案

您的变量 first_array 是一个 int,因为它是您的列表 x 的一个元素(它们都是整数)。如果您不对函数参数使用与主脚本中的变量相同的名称,您会发现调试起来更容易。

关于python - 它在哪里被视为一个整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57641284/

相关文章:

python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令

python - 使用 gevent-socketio 和 Socket.IO.js 的 Python 瓶微框架的最小示例

python - * : 'numpy.ndarray' and 'numpy.float64' 不支持的操作数类型

python - 按索引复制 numpy 数组的单个轴

python - 验证 sqlalchemy session

python - 正则表达式既向后看又向前看

python - 从 Python3 写入 "table"

python - 在Python中使用外部图像

python - 尝试将对象转换为 DateTime,出现 TypeError

python - 使用 pyinstaller 和 numpy 的可执行文件的大小