2012-09-04 110 views
-1

我一直在研究一個Numpy腳本,它突然開始動作了。 我在開頭寫了For循環行,並且已經測試了大約20次左右的腳本,沒有任何問題。現在我認爲其他問題已經解決,Python告訴我「for」語法是錯誤的。有人有任何想法嗎? 確切的Python的輸出是:Numpy Broken「For」Loop?

File "Test.py", line 17 
    for t in range(10): 
    ^

腳本代碼:

#!/Library/Frameworks/EPD64.framework/Versions/7.3 

import numpy as np 
import scipy as sp 

tau = 10 

c = sp.recfromtxt("test.txt") 
binsmax = np.max(c) 

f, dummy = np.histogram(c, bins=(np.arange(binsmax+1)) 

for t in range(tau): 

    if t==0: 
     a = c[:len(c)-1] 
    else: 
     a = c[:-(t+1)] 

    d = c[1:] 
    b = d 
    c = a + b 
    newmax = np.max(c) 

    if binsmax < newmax: 
     binsmax = newmax 

    hist, dummy2 = np.histogram(c, bins=[np.arange(binsmax+1)]) 

    if binsmax < newmax: 
     difference = newmax - binsmax 
     np.append(f, np.zeros(difference)) 
    else: 
     difference = binsmax - newmax 
     np.append(hist, np.zeros(difference)) 
    e = f 
    f = hist + e     # 'f' is the running histogram 

    sp.savetxt(str(t)+"c.txt", c) 
    sp.savetxt(str(t)+"f.txt", f) 

謝謝!

回答

5

你短期在此行密切的括號:

f, dummy = np.histogram(c, bins=(np.arange(binsmax+1)) 
+0

瘋狂的快速響應,以及絕對正確的。謝謝! For循環是固定的。現在我可以回到剩下的廢話了。 :) – Wes

+2

你會找到更好的。對於許多編程語言和編譯器來說,檢查錯誤上面的行是值得的。 – Collin