'列表'对象在列表理解中不可调用

时间:2017-09-15 04:17:20

标签: python list-comprehension

我正在构建这个代码并构建了第一部分,输出了我想要的情节,然后我想在情节的第二部分工作,也许在10个左右之后运行我的前半部分代码停止工作。我没有意思做任何事情,但现在我无法取回它,我的'list' object is not callable'循环收到错误for。虽然我正在使用数组,但它说这个错误。我已尝试使用列表推导的不同语法,以及使数组成为set,list和string。不确定该做什么,所以任何帮助或尝试的事情都会有所帮助。

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 

4 个答案:

答案 0 :(得分:2)

似乎你想调用定义的函数来获取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]

答案 1 :(得分:0)

我认为你应该使用方括号。我的意思是x1[t]

答案 2 :(得分:0)

括号用于调用对象(如果它们是可调用的);对于下标,您必须使用方括号:

取代:

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

y1 = [y1[t] for t in t1]

答案 3 :(得分:0)

x1 = [x1(t) for t in t1]行,x1绑定到list,这是您在def x1 (t):定义的函数。然后,您尝试在x1再次致电x2 = [x1(t) for t in t2]。这意味着您将变量传递给list。您可能希望将函数x1y1重命名为其他名称。