gpt4 book ai didi

python - “列表”对象在列表理解中不可调用

转载 作者:行者123 更新时间:2023-12-01 02:35:53 24 4
gpt4 key购买 nike

我正在构建这段代码并构建了输出我想要的绘图的第一部分,然后我开始处理绘图部分的第二半部分,也许在运行 10 左右后我的代码的前半部分停止了在职的。我本来不想做任何事情,但现在我无法取回它,并且我的 for 循环收到错误 'list' object is not callable' 。尽管我使用的是数组,但它说这个错误。我尝试了不同的列表理解语法以及将数组设置为集合、列表和字符串。不太确定该怎么做,所以任何帮助或尝试的事情都会有帮助。

import numpy as np
import pylab as plt

#Before the explosion
t1 = np.asarray(range(0, 5))
t2 = np.linspace(0, 4 , 1)
g = 1.0
vx = 4.0
vy = 4.0

def x1 (t):
return (vx*t)

def y1 (t):
return(vy*t -(0.5*g*(t**2.0)))

x1 = [x1(t) for t in t1]
y1 = [y1(t) for t in t1]

x2 = [x1(t) for t in t2]
y2 = [y1(t) for t in t2]

#after the explosion

'''
t3 = range(5,10)
t4 = np.linspace(5, 9 , 1000)

vx = 5
vy = 3

def x2 (t):
return (16+vx)

def y2 (t):
return(vy*t -(0.5*g*(t**2)))

'''
plt.scatter(x1,y1, color='blue',marker='+',s=100.0, label = '')
plt.plot(x2,y2, color='red',marker=None, label = '')

plt.show()

输出:

     20 y1 = [y1(t) for t in t1]
21
---> 22 x2 = [x1(t) for t in t2]
23 y2 = [y1(t) for t in t2]
24

TypeError: 'list' object is not callable

最佳答案

您似乎想调用定义的函数来获取 x2 的值。尝试更改定义中函数的名称(或更改变量 x1 和 y1 的名称)。

def xfunc(t):
return (vx*t)
def yfunc(t):
return(vy*t -(0.5*g*(t**2.0)))

x1 = [xfunc(t) for t in t1]
y1 = [yfunc(t) for t in t1]

x2 = [xfunc(t) for t in t2]
y2 = [yfunc(t) for t in t2]

关于python - “列表”对象在列表理解中不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46231622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com