'list' object is not callable in list comprehension
I was building this code and built this first part that outputted the plot that I wanted, then I wen to work on the 2nd half of the portion of the plot and maybe after 10 or so runs my first half of my code stops working. I didn't mean to do anything but now I can't get it back and I get the error
'list' object is not callable' for my
for loop. It says this error although I am using an array. I have tried different syntax with the list comprehension as well as making the array a set, list, and string. Not really sure what to do so any help or things to try would be helpful.
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()
作者: ripchint 的来源 发布者: 2017 年 9 月 15 日
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
i think you should use square brackets . i mean
Parenthesis are used to call objects (if they are callable); for subscripting, you must use square brackets:
y1 = [y1(t) for t in t1]
作者: Reblochon Masque 发布者: 15.09.2017 04:21
y1 = [y1[t] for t in t1]
It seems you want to call the function defined to get the values for x2. Try changing the name of the functions in the definition (or change the name of the variables x1 and y1) .
作者: user8612543 发布者: 15.09.2017 04:38
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]
At the line of
x1 = [x1(t) for t in t1] you bound
x1 to a
list, which was a function as you defined at
def x1 (t):. Then you tried to call
x1 again at
x2 = [x1(t) for t in t2]. This means you're passing a variable to a
list. Maybe you want to rename your function
y1 to other names.