2017-09-15 138 views
1

我正在構建這個代碼,並構建了輸出我想要的情節的第一部分,然後我開始處理情節的第二部分,也許在10或者如此運行我的代碼的第一半停止工作。我不是故意要做任何事情,但現在我無法恢復,並且我的for循環出現錯誤'list' object is not callable'。它說這個錯誤,雖然我正在使用一個數組。我已經嘗試了與列表理解不同的語法,並使數組成爲一個集合,列表和字符串。不確定該怎麼做,所以任何幫助或嘗試都會有所幫助。'list'object is not callable in list comprehension

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 
+1

您已經定義了'x1'兩次。這是一個功能,也是一個列表。我想你正試圖調用這個函數,然後它被一個列表所取代。 –

+0

是的,所以我改變它只是'x(t)',並在循環中調用它'x(t)'並且它工作。不知道爲什麼,但它的工作。 – ripchint

回答

0

我認爲你應該使用方括號。我的意思是x1[t]

0

括號用於調用對象(如果它們是可調用的);爲下標,則必須使用方括號:

取代:

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

y1 = [y1[t] for t in t1] 
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] 
+0

是的,這工作。我試圖改變它,以區分它與我將在下一部分中調用的新功能。 – ripchint

0

在的x1 = [x1(t) for t in t1]線你結合x1list,因爲你在def x1 (t):定義這是一個功能。然後您嘗試再次撥打x1,電話號碼爲x2 = [x1(t) for t in t2]。這意味着你將一個變量傳遞給list。也許你想將你的功能x1y1重命名爲其他名稱。