我看过类似问题的答案,但在我的代码中找不到将 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/