2015-11-04 105 views
-3

我試圖從遞歸循環創建列表。我知道它能夠產生我需要的結果,因爲我已經通過打印結果來檢查結果。Python - 從遞歸循環創建列表

這裏是我的遞歸循環:

def move_forward_occurences(occurrences, firstListDt): 
    listingResults = [] 
    for x in range(0, occurrences): 
     firstListDt = firstListDt + relativedelta(bdays=+2) 
     listingResults.extend(firstListDt) 
     return listingResults 

另外,該問題可能是因爲我沒有正確地檢查它:

if occurrences != 0: 
    listingResult = move_forward_occurences(occurrences, firstListDt) 
    for result in listingResult: 
     print(result) 

參數的說明,如果需要(他們很自我已解釋): 出現=產生結果的次數 firstListDt =開始日期

謝謝提前!

+1

循環......不......遞歸 –

+2

「我知道這是能夠產生我需要的結果,因爲我已經檢查通過打印結果。「那麼,問題是什麼? – Evert

回答

0

您的return已過度縮進。你想推遲返回,直到for循環結束後:

def move_forward_occurences(occurrences, firstListDt): 
    listingResults = [] 
    for x in range(0, occurrences): 
     firstListDt = firstListDt + relativedelta(bdays=+2) 
     listingResults.extend(firstListDt) 
    return listingResults