Python TypeError:不支持**或pow()的操作数类型:';列表';和';int';。plt.plot
当我编写以下代码时,我得到以下错误。 我做错了什么Python TypeError:不支持**或pow()的操作数类型:';列表';和';int';。plt.plot,python,Python,当我编写以下代码时,我得到以下错误。 我做错了什么 def f(t): return np.sin(t**2) n = 20 # number of points for Riemann integration a = 0; b = 2 P = np.linspace(a, b, n) # Standard partition constant width dt = (b-a)/n T = [np.random.rand()*dt + p for p in P[:-1]] # R
def f(t):
return np.sin(t**2)
n = 20 # number of points for Riemann integration
a = 0; b = 2
P = np.linspace(a, b, n) # Standard partition constant width
dt = (b-a)/n
T = [np.random.rand()*dt + p for p in P[:-1]] # Randomly chosen point
再做几件事,然后:
最后我得到以下错误:
Traceback (most recent call last):
File "riman.py", line 35, in <module>
plt.plot(T, f(T), '.', markersize=10)
File "riman.py", line 11, in f
return np.sin(t**2)
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
回溯(最近一次呼叫最后一次):
文件“riman.py”,第35行,在
plt.绘图(T,f(T),“.”,markersize=10)
文件“riman.py”,第11行,在f中
返回np.sin(t**2)
TypeError:不支持**或pow()的操作数类型:'list'和'int'
错误消息是不言自明的–没有为内置的列表
类型定义电源操作员**
但是,它们是为np.array
s定义的:
>>> np.array(range(5))**2
array([ 0, 1, 4, 9, 16])
修正:
t
是一个列表……我该怎么做?@BatelCarmona点击左侧的灰色勾号,就在分数下面
>>> np.array(range(5))**2
array([ 0, 1, 4, 9, 16])
T = np.array([np.random.rand()*dt + p for p in P[:-1]])